Support » Fixing WordPress » Custom loop on page – comes up on top of page

  • Resolved milanouser


    I know, similar questions have been asked in many ways, but I simply cannot find any answer.

    I have a page (book) with 4 tabs:
    – Summary (=page content)
    – Look inside
    – Reviews
    – Buy it

    The tab “Reviews” should be a custom loop of specific posts (book reviews) (using post type, and order by custom field: publication date)

    The reason it has to be a page template and not category template is that the site owner must have frontend access to write some content, without messing with the rest, or having to work directly on a template file.

    What I have come up with so far does work, but shows the custom loop on top of page instead of inside the tab!

    (Note: I use shortcode for tabs, so the first [tab] part comes from the page content and needs to be finished by the template. That part is not the problem)

    This is my code:

    get_header(); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <h1 class="page-title"><?php the_title(); ?></h1>
      <?php global $post;  // get post ID for reuse after custom loop
      $tmp_post = $post; ?>
      <?php // get content from frontend
      $content = get_the_content()
      $content .= '[/tab]
      [tab title="Look inside" id="td2"]
           link to Google docs file
      [tab title="Reviews" id="3"]'
        $query = array (
               'post_type' => 'reviews',
               'meta_query' => array(
                               array('key' => 'wpcf-book-review',
                                     'value' => '1',
                                     'compare' => '='
               'order-by' => 'meta-value-num',
               'meta-key' => 'wpcf-pub-data',
               'order' =>'ASC'
        $my_query = new WP_Query($query);
        if ( $my_query->have_posts() ) :
           while ( $my_query->have_posts()) :
                 $loopcontent .=  get_template_part( 'includes/loop-review');
      $content .= $loopcontent .
      [tab title="Buy it" id="t4"]
          Links to bookstores
      echo apply_filters('the_content',$content);
    //reset post ID to main page
    $post = $tmp_post;
    // all the rest: comment form, sidebar, etc.

    This code does concatenate nicely the different tab bits from page content and rest of the template, only the custom loop does not remain within the tab, but shows up right after the_title (from page).

    I have tried the Blog-in-blog plugin, and it does indeed put the custom loop inside the correct tab!
    Just that it does not allow to do advanced queries and display order the way I want it (and my PHP knowledge is not that advanced that I can mess around with the code there).

    Any help is greatly appreciated to solve that mystery why my custom loop jumps to the top of the page!

    Thank you in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael


    Forum Moderator

    get_template_part() is basically only linking the file – it is not returning the results or whatever that file/template outputs.

    you might need to work with output buffering;

    for example,
    replace this line:

    $loopcontent .=  get_template_part( 'includes/loop-review');


    get_template_part( 'includes/loop-review');
    $loopcontent .=  ob_get_contents(); ob_end_clean();


    @alchymyth – you are a genius!

    Thank you for the swift – and working – reply! Buffering did the trick! Hope that will not have too much impact on performance, though. Any suggestions here?

    Just for clarification: of course using get_template_part() is only for layouting the output, but even when I used some minimal markup, say:
    $loopcontent .= the_title() . '<br>'
    it did spit out these post titles from the custom loop at the top of the page. Now with buffering the loop I get it at the right place.

    I used the variable $loopcontent because ultimately the custom query and loop should go into a function, with query variables passed from the main page/template.

    Thanks again for the solution.



    Forum Moderator

    using the_title() for instance is a bad example as it outputs the title immediately, instead of returning it as get_the_title() or the_title('','',false) would do.

    a lot of WordPress functions are similar:
    the_whatever() would output = echo the result;
    get_whatever() or get_the_whatever() would return the result.

    in your case, this should have worked:

    $loopcontent .= get_the_title() . '<br>';
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom loop on page – comes up on top of page’ is closed to new replies.