Site logo

[Resolved] Navigation on custom loops (WP Pagenavi)

Home Forums Support [Resolved] Navigation on custom loops (WP Pagenavi)

Home Forums Support Navigation on custom loops (WP Pagenavi)

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2301669
    Tesco

    I have a custom loop that uses Wp PageNavi plugin but it shows the navigation at the top of the page instead of showing it at the bottom as it should be.

    Therefore I have two questions:

    Any reason why this is happening?

    The theme as custom navigation built in (if so, how can I call it in a shortcode)?

    #2301747
    David
    Staff
    Customer Support

    Hi there,

    how is the custom loop created ? Can you share a link to where i can see it ?

    #2301765
    Tesco

    Hi David,

    Sorry, I cannot share a link mainly for two reasons: I’m working on localhost and my client has strict rules.

    But I can share the loop with you, if that helps..?

    Also:

    Maybe (just maybe), this can be related?

    #2301831
    David
    Staff
    Customer Support

    In that other topic, you have multiple loops, if each loop requires its own pagination then that can be done IF the GB Query Loop block is used. It was built with that in mind and can handle multiple paginated loops.

    If you’re not using that, then it requires Custom Development.

    #2301841
    Tesco

    The shortcode is used (in the hook) generate_after_content. So, in fact I just have one loop.

    Why is the wp navigation div ir rendered above the page and not at the end of the loop?

    Here’s my loop in the shorcode:

    if ( get_query_var('paged') ) {
                    $paged = get_query_var('paged');
                } elseif ( get_query_var('page') ) { 
                    $paged = get_query_var('page');
                } else {
                    $paged = 1;
                }
    
    $posts_qt = 10;
    $args = array(
      'showposts' => $posts_qt,
      'paged' => $paged,
      'orderby' => 'desc',
       );
    
    $temp = $home_query;
    $home_query= null;
    $home_query= new WP_Query( $args );
    
     if ($home_query->have_posts()) : while ($home_query->have_posts()) : $home_query->the_post();
    
    //The loop goes here
     
     endwhile; 
    
    			if(function_exists('wp_pagenavi')) :
    			    wp_pagenavi( array( 'query' => $home_query) );
    			else : 
    			    echo '<div class="navigation">';
    				    echo '<div class="alignleft">'.previous_posts_link().'</div>';
    				    echo '<div class="alignright">'.next_posts_link().'</div>';
    			    echo '</div>';
     endif; 
    
        wp_reset_postdata();
    else:
        wp_redirect( home_url(), 302); exit; 
    endif;
    #2301905
    David
    Staff
    Customer Support

    That would be hard for me to say, as i don’t see the entire picture and custom development is outside of our scope
    One issue that may cause a problem is is using echo to output content.
    Shortcodes should only ever return content not output it.

    To use echo inside a shortcode you would need to use object buffering – see here for an example:

    https://docs.generatepress.com/article/creating-a-shortcode/

    #2301928
    Tesco

    David,

    You where very useful!

    Following your tips resolved my issue.

    Thank you!

    #2302156
    David
    Staff
    Customer Support

    Glad to be of help!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.