WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom loop on page - comes up on top of page (4 posts)

  1. milanouser
    Member
    Posted 1 year ago #

    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]
      [tab title="Reviews" id="3"]'
    
    <?php // THIS IS THE PROBLEM PART
    
        $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()) :
                 $my_query->the_post();
                 $loopcontent .=  get_template_part( 'includes/loop-review');
           endwhile;
        endif;
    
      $content .= $loopcontent .
      '[/tab]
      [tab title="Buy it" id="t4"]
          Links to bookstores
      [/tab]
      [/tabgroup]';
    
      echo apply_filters('the_content',$content);
    
    //reset post ID to main page
    $post = $tmp_post;
    wp_reset_postdata();
    
    // 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!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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');

    with:

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

    (untested)

    http://codex.wordpress.org/Function_Reference/get_template_part

  3. milanouser
    Member
    Posted 1 year ago #

    @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.

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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.

    http://codex.wordpress.org/Function_Reference/the_title
    http://codex.wordpress.org/Function_Reference/get_the_title

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

    in your case, this should have worked:

    $loopcontent .= get_the_title() . '<br>';

Topic Closed

This topic has been closed to new replies.

About this Topic