admin管理员组文章数量:1430551
I'm building a custom shortcode in Wordpress. Essentially what I want this to do is the following:
- Dynamically pull values from a group of Advanced Custom Fields
- Output those values into a javascript array, comma-separated
Where I'm getting stuck is that I can successfully run a loop over the group of fields and grab all the values and output them on a page. However, when I attempt to store them into a variable to be pulled into a javascript array ($v
is stored in the output
variable which is referenced in the urls
array), it only returns one of those values.
How can I get all the values from the foreach ( $value as $v)
to list inside of the urls
array, with comma separation? Any help would be appreciated :)
add_shortcode('training-player', 'training_player');
// Add shortcode for Diagnostic media playlist
function training_player() {
$author_id = get_the_author_meta('ID');
$fields = acf_get_fields(405);
if( $fields ) {
echo '<div id="player"></div>';
foreach( $fields as $field ) {
$value = get_field( $field['name'], 'user_'. $author_id );
if(!empty($value)) {
if ($value) {
foreach ( $value as $v) {
echo $v;
}
}
}
}
echo "
<script src='.js/vimeowrap.js'></script>
<script src='.js/vimeowrap.playlist.js'></script>
<script>
var output = '$v';
console.log(output)
vimeowrap('player').setup({
urls: [
output
],
plugins: {
'playlist':{}
}
});
</script>
";
}
}
I'm building a custom shortcode in Wordpress. Essentially what I want this to do is the following:
- Dynamically pull values from a group of Advanced Custom Fields
- Output those values into a javascript array, comma-separated
Where I'm getting stuck is that I can successfully run a loop over the group of fields and grab all the values and output them on a page. However, when I attempt to store them into a variable to be pulled into a javascript array ($v
is stored in the output
variable which is referenced in the urls
array), it only returns one of those values.
How can I get all the values from the foreach ( $value as $v)
to list inside of the urls
array, with comma separation? Any help would be appreciated :)
add_shortcode('training-player', 'training_player');
// Add shortcode for Diagnostic media playlist
function training_player() {
$author_id = get_the_author_meta('ID');
$fields = acf_get_fields(405);
if( $fields ) {
echo '<div id="player"></div>';
foreach( $fields as $field ) {
$value = get_field( $field['name'], 'user_'. $author_id );
if(!empty($value)) {
if ($value) {
foreach ( $value as $v) {
echo $v;
}
}
}
}
echo "
<script src='https://luwes.github.io/vimeowrap.js/vimeowrap.js'></script>
<script src='https://luwes.github.io/vimeowrap.js/vimeowrap.playlist.js'></script>
<script>
var output = '$v';
console.log(output)
vimeowrap('player').setup({
urls: [
output
],
plugins: {
'playlist':{}
}
});
</script>
";
}
}
Share
Improve this question
edited Apr 30, 2019 at 15:44
Noah
asked Apr 30, 2019 at 13:58
NoahNoah
11 bronze badge
5
|
2 Answers
Reset to default 0This is not really a Wordpress question. However you probably need the implode php function, using a comma as glue.
foreach( $fields as $field ) {
$value = get_field( $field['name'], 'user_'. $author_id );
if(!empty($value)) {
if ($value) {
$v = implode(',', $value);
}
}
}
This might get your desired output:
<?php
add_shortcode('training-player', 'training_player');
// Add shortcode for Diagnostic media playlist
function training_player() {
$author_id = get_the_author_meta('ID');
$fields = acf_get_fields(405);
$js_values = [];
if( $fields ) {
echo '<div id="player"></div>';
foreach( $fields as $field ) {
$value = get_field( $field['name'], 'user_'. $author_id );
if(!empty($value)) {
if ($value) {
foreach ( $value as $v) {
$js_values[] = $v;
}
}
}
}
echo "
<script src='https://luwes.github.io/vimeowrap.js/vimeowrap.js'></script>
<script src='https://luwes.github.io/vimeowrap.js/vimeowrap.playlist.js'></script>
<script>
var output = " . wp_json_encode( $js_values ) . ";
console.log(output)
vimeowrap('player').setup({
urls: [
output
],
plugins: {
'playlist':{}
}
});
</script>
";
}
}
We're storing the value of $v
in the $js_values
array, and then outputting it in a format workable with Javascript using wp_json_encode
instead of trying to directly put it into Javascript.
For future reference, you may want to look at [wp_localize_script](https://developer.wordpress/reference/functions/wp_localize_script/)
for getting values from PHP to Javascript "the WordPress way", and enqueue the rest of your JS using [wp_enqueue_script](https://developer.wordpress/reference/functions/wp_enqueue_script)
, but that's beyond the scope of this answer.
本文标签: phpHow to output values from a loop into a javascript array
版权声明:本文标题:php - How to output values from a loop into a javascript array 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745534132a2662195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$v_all = [];
beforeforeach( $fields as $field )
, replaceforeach ( $value as $v) { ... }
with$v_all[] = implode(',', $value);
, and aboveecho
add$v = implode(',', $v_all);
. – nmr Commented Apr 30, 2019 at 16:11array_walk( $value, function(&$item) { $item = '"' . $item . '"'; } );
added before$v_all[] = implode(',', $value);
. – nmr Commented Apr 30, 2019 at 16:35output
variable not being being parsed correctly when it's brought into the JSON. – Noah Commented Apr 30, 2019 at 17:23