Wordpress Pagination Not Working On Custom Page Template - php

I cannot get pagination links to appear on a custom page template. I have followed the Wordpress docs and have made this work before but for some reason the following code won't output the pagination links.
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'category_name' => 'press-releases',
'paged' => $paged,
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>
<div class="previous-posts">
<?php next_posts_link('Previous Posts'); ?>
</div>
<div class="newer-posts">
<?php previous_posts_link('Newer Posts'); ?>
</div>
<?php endif; wp_reset_postdata(); ?>
Hoping another set of eyes on this can spot an issue and help find a fix.
Thank You!

Related

Wordpress second page pagination not showing posts

I've been trying to add numeric pagination to my custom wordpress theme. I have run into a problem where I can't see any posts on second(or third page). I have my page-archive.php file, index.php, single.php files all set up. It should be everything that a blog site needs? I'm a bit confused what am I missing here? I've been trying multiple different options and I tried to modify my page-archive.php page but no luck.
index.php
<div class="blogitem a">
<?php
//PRINT ONLY Tutvustus
$lastBlog = new WP_Query('type=post&posts_per_page=3');
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
<?php get_template_part('page-archive',get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php // post content goes here ?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>
</div>
functions.php
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
page-archive.php
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
It turns out that I don't need the first half of the code. Feeling dumb. Anyways here is the answer.
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?> // added template part here and voila it works
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>

Wordpress custom pagination shows same posts on every page

I have been trying to add custom numeric pagination to my custom Wordpress theme. Everything seems good so far but the problem is that every page shows the same 3 posts. Is there something I should consider doing while building my own Wordpress blog theme. Right now I have my page-archive.php and single.php file there, do I need something else for this to work? Also filtering with category isn't working, it keeps sending me back to index.php
Code in my index.php file
<div class="blogitem a">
<?php
//PRINT ONLY Tutvustus
$lastBlog = new WP_Query('type=post&posts_per_page=3');
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
<?php get_template_part('page-archive',get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php endif; ?>
</div>
</div>
Code in my functions.php file
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
I modified my page-archives.php file to this code.
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
Now my filtering with category is working but if I choose second page from the pagination it doens't show any posts.
I think you need wp_reset_query... some helpful links:
https://developer.wordpress.org/reference/functions/wp_reset_query/
https://developer.wordpress.org/reference/functions/query_posts/
see example here:
<div class="blogitem a">
<?php
//PRINT ONLY Tutvustus
$lastBlog = new WP_Query('type=post&posts_per_page=3');
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php endif; ?>
<?php wp_reset_query(); // add this ?>
</div>
</div>
It turns out I am silly and I don't need the first half of the code anyways.
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?> // added template part here and voila it works
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>

Wordpress Pagination on static homepage

I am trying to get Pagination working on my static homepage which I have integrated with wordpress. The problem I am having is when I click the "Older Entries" button on the page it goes to the ?paged=2 page but displays the first 10 posts still. Just like on the first page.
I know the code somewhat works because I changed ($the_query->max_num_pages > 1) to 2 and went to the page and it displayed the contents of the second page with a "Newer Entries" button.
I just cannot get it to do it automatically.
I used this persons tutorial on how to set it up
http://callmenick.com/post/custom-wordpress-loop-with-pagination
and here is my code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h1><?php echo the_title(); ?></h1>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
The website (temporary) can be seen here http://auroraservers.org/MainSite/Index.php
So you can see what it is doing.
Thank you for any help! I've been trying to fix this for over a day now so I hope it isn't too much trouble.
This line should be paged instead of page
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

posts per page on a static page + pagination

Thanks for taking the time to check this out.
I've spend a couple days going over this. Lots of time on wp.org but i'm just not getting it. Should be an easy fix I'm sure. No matter what i try, i can't limit the number of posts per page nor get any pagination to display. This is my most recent attempt (maybe not my best attempt). The page just shows all the posts OR all the recent ones or something (not my site). Once i can at least get the page to limit the posts, then i'll tackle the pagination. Also, setting the posts per page in the WP dashboard does nothing...and never did. That's why i'm trying to code something myself. Why can't i limit the number of posts per page? Would i put the pagination where i currently have it? Is this a total mess, lol?
Thanks again,
Dave (code below)
<?php /* Template Name: Stories */ ?>
<?php
get_header(); ?>
<!-- *************************************** -->
<div class="custom_two_third">
<?php
// The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
}
// pagination
next_posts_link();
previous_posts_link();
} else {
get_template_part( 'template-parts/content', 'none' );
}
/* Restore original Post Data */
wp_reset_postdata();
?>
<div class="clear"></div>
</div><!-- custom_two_third -->
<?php
get_sidebar();
get_footer();
?>
Try using post_type
$args_articles =array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'order' => 'desc',
'paged' => $paged
);
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_eve_args = array( 'post_type' => 'news_events', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $news_eve_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no news or events at this time.', 'theme' ); ?></p>
<?php endif; ?>
Try this one, it will work in your case.
global $query_string;
query_posts("{$query_string}&posts_per_page=12");
while (have_posts()) {
the_post();
...
}

Wordpress Pagination wp_query

I have a custom page which lists ALL of the posts no matter the category, I have hit a brick wall with the pagination! for some reason the pagination ins't showing up!
Here is my code
<?php // Template Name: News Feed ?>
<?php get_header(); ?>
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-3">
<div id="left-sidebar">
<?php get_sidebar('posts') ?>
</div>
</div>
<div class="col-md-6">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
<div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
</div>
</div>
<div class="col-md-3">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
Your links will not show up because next_posts_link() is set to the $max_num_pages parameter of the main query ($wp_query->max_num_pages) by default. On pages, this will always be 1 and by default these links don't display when there is only one page
Also, you are not paginating your query, so even if you gt your links working, you will see the same posts being repeated.
You should add the paged parameter to your query like this
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
// Rest of you arguments
);
and then you need to alter the $max_pages parameter in next_posts_link() like this
next_posts_link( 'Next entries', $my_query->max_num_pages );
First try to add 'posts_per_page' parameter into the $args, so the loop will know how much posts to display in the page.
Second,From WordPress Codex :
If the pagination is broken on a static front page you have to add the "paged" >parameter this way:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts('posts_per_page=3&paged=' . $paged);
?>
That means Just add this code before your args (without the "query_posts") and then but the args like this :
$args = array(
'post_type' => 'post',
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
I am recommending to explore the codex once again, maybe there is something there that can help you.
http://codex.wordpress.org/Pagination#Example_Loop_with_Pagination
You have created custom template so you should try with the following link first:
https://wordpress.org/support/topic/pagination-not-working-on-custom-template-1
Also, I request you to go through this link and it explain everything in details
By default, in any given context, WordPress uses the main query to determine pagination. The main query object is stored in the $wp_query global, which is also used to output the main query loop:
if ( have_posts() ) : while ( have_posts() ) : the_post();
When you use a custom query, you create an entirely separate query object:
$custom_query = new WP_Query( $custom_query_args );
And that query is output via an entirely separate loop:
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
But pagination template tags, including previous_posts_link(), next_posts_link(), posts_nav_link(), and paginate_links(), base their output on the main query object, $wp_query. That main query may or may not be paginated. If the current context is a custom page template, for example, the main $wp_query object will consist of only a single post - that of the ID of the page to which the custom page template is assigned.
If the current context is an archive index of some sort, the main $wp_query may consist of enough posts to cause pagination, which leads to the next part of the problem: for the main $wp_query object, WordPress will pass a paged parameter to the query, based on the paged URL query variable. When the query is fetched, that paged parameter will be used to determine which set of paginated posts to return. If a displayed pagination link is clicked, and the next page loaded, your custom query won't have any way to know that the pagination has changed.
https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
I hope this will work for you
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Would you please try above code?

Categories