开发者

What's a safe way to declare a variable so I can call it from another script?

开发者 https://www.devze.com 2023-04-10 15:51 出处:网络
I\'ve built an array of image URLs on a Wordpress template page like this: <?php $attachments = get_posts(array(

I've built an array of image URLs on a Wordpress template page like this:

<?php $attachments = get_posts(array(
        'order'          => 'ASC',
        'post_type'      => 'attachment',
        'post_parent'    => $post->ID,
        'post_mime_type' => 'image',
        'post_status'    => null,
        'numberposts'    => -1,
    ));
    if ($attachments) {
        //set up array of urls
        $image_urls = array();
        foreach($attachments as $attachment){
            $image_object = wp_get_attachment_image_src($attachment->ID,'full');
            $image_urls[] = $image_object[0];
        }
    } ?>

Then, in footer.php, I'd like to print the array for Javascript like this:

<script>
var images = [<?php $num_urls = count($image_urls);
                    $num = 1;
                    foreach($image_urls as $image_url) {
                        echo $image_url;
                        $num++;
                        if($num<$num_urls) echo ', ';
                    } ?>];
</script>

I mistakenly assumed that, in concatenating the template page and footer.php, PHP would view the script as continuous, and开发者_StackOverflow社区 remember the variable value, but that's not the case, as it returns:

Warning: Invalid argument supplied for foreach()

How do I declare that $image_urls array so that I can refer to it later, without a scoping/namespacing danger?

PS Is there a better way to add a comma after all but the last item in the latter piece of code?


To safely pass the $image_urls around in multiple scripts, declare it like this:

$GLOBALS['image_urls'] = array();
foreach($attachments as $attachment){
    $image_object = wp_get_attachment_image_src($attachment->ID,'full');
    $GLOBALS['image_urls'][] = $image_object[0];
}

And later, to reference it:

<script>
var images = [<?php $num_urls = count($GLOBALS['image_urls']);
                $num = 1;
                foreach($GLOBALS['image_urls'] as $image_url) {
                    echo $image_url;
                    $num++;
                    if($num<$num_urls) echo ', ';
                } ?>];
</script>

To make sure you do not conflict with anything, you can add a prefix to image_urls to indicate that it is yours.


I mistakenly assumed that, in concatenating the template page and footer.php, PHP would view the script as continuous, and remember the variable value

It should, unless Wordpress is odd this way, or unless the variable is inside a function scope (in either file). If so, add this declaration:

global $image_urls;

at the top of the function(s). Alternatively, reference $image_urls everywhere via $GLOBALS. E.g.,

$GLOBALS['image_urls'][] = $image_object[0];

Is there a better way to add a comma after all but the last item in the latter piece of code?

Use the implode function:

echo implode(', ', $image_urls);

I think you need quotes around each item too, so:

if (count($image_urls)) {
    echo '"' . implode('", "' $image_urls) . '"';
}

Or, in general, I'd do something like this for that type of loop:

$first = true;
foreach (...) {
    if ($first) $first = false; else echo ', ';
    echo $item;
}


Script looks fine.

Though, this would be better:

<script>
var images = [<?php echo implode(',',$image_urls) ?>];
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号