2

I have a custom function to query some posts. I was wondering if it is okay to echo variables within the loop of the function, since normally I would echo a variable at the end of the function. If so, how to properly store multiple values in that variable?

See example code below:

<?php 
function get_some_posts($args) { 
        $query = new WP_Query( $args );

        if ( $query->have_posts() ) {

            while ( $query->have_posts() ) {

                $query->the_post();
                //Can i echo variables here?
                get_template_part( 'parts/content-item', get_post_type() );

                //Or should i put it into a variable and if so how? Since the below is not working.
                $to_echo = get_template_part( 'parts/content-item', get_post_type() );
            }

        }
        //And then echo it at the end of the function?
        echo $to_echo;
}
add_action('custom_hook','get_some_posts');
1

Generically speaking, yes. If your normal output would be to echo at the end, echoing during the loop wouldn't change much.

function get_some_posts($args) { 
        $query = new WP_Query( $args );

        if ( $query->have_posts() ) {

            while ( $query->have_posts() ) {

                $query->the_post();
                the_title(); // the_title() is the same as echo get_the_title();
            }

        }
}
add_action('custom_hook','get_some_posts');

The output of the above should be the same as the output below:

function get_some_posts($args) { 
        $query = new WP_Query( $args );

        if ( $query->have_posts() ) {

            // declare your variable to avoid php output errors
            $to_echo = '';

            while ( $query->have_posts() ) {

                $query->the_post();

                $to_echo .= get_the_title(); // ".=" adds to the current value of the variable.
            }

        }
        //And then echo it at the end of the function?
        echo $to_echo;
}
add_action('custom_hook','get_some_posts');

Of course, I'm working straight off of your question's code. This lacks a little context and you would also want to apply some sort of space or line break between the titles.

By way of practical example, and your question about not echoing it in The Loop, here's a possibility just as an example:

add_filter( 'the_content', 'get_some_posts' );
function get_some_posts( $content ) { 

    if ( is_single() ) {

        $query = new WP_Query( array( 'post_type' => 'page' ) );

        if ( $query->have_posts() ) {

            // declare your variable to avoid php output errors
            $to_echo = '';

            while ( $query->have_posts() ) {

                $query->the_post();

                $to_echo .= get_the_title() . "<br />";
            }

        }

        $content = $content . "<p>Checkout some pages:</p>" . $to_echo;
    }

    return $content;
}

This would take your "theoretical" and apply it to WP's "the_content" filter which filters the main content area. It first checks if we're on a single post, and if so, runs a query to get pages. It puts the titles of those pages into a list, adds some text to it, then tacks in onto the $content variable (coming through the filter) before sending back the result.

It's not a "real world" example in terms of final use because I didn't actually include links - just the titles, and it's not limited in number so it could be a ridiculous amount of extra content. It's merely here as an example for your question of "how" can you take a result from WP_Query and return it. So it's an example of what "filters" do (or might do). If it were an action, then that would be different. You'd either have to echo it in the function OR put it into a variable declared as a global and pick that global up in a later action/filter.

  • Thanks. But is it bad to echo in a function, within the loop? I want to call a template_part within the loop and I am not sure how to echo this at the end of the function. Of course, calling it within the loop is no problem See my edited question. How to properly do this within the function? Thanks. – RobbTe Nov 6 '18 at 10:27
  • 1
    Is it bad to echo in a function within the loop? That's too broad a question - it depends. Your question uses "custom_hook" so it depends on the hook. Also, you've got it as an action, so if there's any output to do, then you'd have to echo it. Actions don't return anything, they allow you to do something. If it were a filter, then depending on the filter and the expected outcome, you probably would want to return a value rather than echo it, but again that depends entirely on the expected result. – butlerblog Nov 7 '18 at 13:38
  • Yes okay, thanks. Meaning for a filter you need to return it at the end of the function, and for an action it's okay to echo it within a loop, like the example? – RobbTe Nov 7 '18 at 13:39
  • 1
    More-or-less, yes. I added a more practical example above using a real filter (the_content) to demonstrate how one might do that - getting some page titles and tacking them onto the end of a single post's content. Also, in an action, there is the possibility of putting your result into a global variable to be picked up in another function later, so with an action you might echo a result (if needed/desired) right away OR you might do something else with it. Of course it still depends on the real world application necessary. – butlerblog Nov 7 '18 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.