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

  • Home
  • SEARCH
  • 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 7505573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:57:47+00:00 2026-05-29T21:57:47+00:00

I want to wrap my code in a function (and then put it in

  • 0

I want to wrap my code in a function (and then put it in functions.php) so that I can call it elsewhere but my code fails as soon as I wrap it in a function.

I think this may be a scope issue, do I have to pass the the post number somehow to the function? If I get rid of the function that’s wrapped around the query, the code works fine.

I’m guessing that the code is irrelevant really (although I may be wrong) – it’s more to do with the fact that it’s a loop and a function.

        <?php function getGallery2() { ?>
            <!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
            <?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>

            <?php while ( have_posts() ) : the_post(); ?>
                <!-- 2. echo the test field -->
                <?php $link =  get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
                <?php $alt =  get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
                <img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
            <?php endwhile;?>

            <?php wp_reset_query(); ?>

        <?php } ?>

        <?php getGallery2(); ?>
  • 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-29T21:57:48+00:00Added an answer on May 29, 2026 at 9:57 pm

    $post is not in the functions scope.

    You can add global $post; to the top of the function or you can include it as a parameter like this:

    function getGallery2($post){
        // code
    }
    
    echo getGallery2($post)
    

    Code inside a function can only see variables that were created within the same function or in global scope. Meaning the $post object is undefined.

    //
    On a slightly off topic note, you have lots of HTML comments within PHP. You could easily tidy things p by making it all PHP.

    EDIT:

    function getGallery2(){
    
        global $post;
    
        // 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
        query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); 
    
        while ( have_posts() ) : the_post(); 
            // 2. echo the test field -->
            $link =  get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); 
            $alt =  get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); 
            echo '<img src="'.$link.'" alt="echo $alt " />';
        endwhile;
    
        wp_reset_query(); 
    
    } 
    
    getGallery2();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to wrap a piece of code that uses the Windows Impersonation API
In jQuery you can wrap all your code in $(function() { ... }); and
I've got a (poorly written) base class that I want to wrap in a
There must be an easy way to do this, but somehow I can wrap
I want to declare a function dynamically and I want to wrap any access
I want to call member function by passing it as template parameter, without using
How can I tell closure compiler that an anonymous function should not be removed
I want to put some jquery that I use for one page in a
Some words pass from search, I want to wrap them with a div then
I want to wrap a Perl one-liner in a batch file. For a (trivial)

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.