Archived topic
Navigation on custom loops (WP Pagenavi)
7 replies · Started by Tesco on August 3, 2022
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)?
Hi there,
how is the custom loop created ? Can you share a link to where i can see it ?
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?
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.
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;
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/
David,
You where very useful!
Following your tips resolved my issue.
Thank you!
Glad to be of help!