Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:04:46+00:00 2026-05-25T22:04:46+00:00

I’ve built an array of image URLs on a WordPress template page like this:

  • 0

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 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:04:46+00:00Added an answer on May 25, 2026 at 10:04 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.