I have been trying to add numeric pagination to my posts page which are filtered by category. The problem is that pagination won't appear when I call it. Pagination works perfectly fine when I use previous_posts_link and next_posts_link but this is not excatly what I want to achieve. What am I missing here?
My filtered category page.
<?php
// Get post ID
$post_type = get_post_type( $post->ID );
// Get category ID
$category_id = get_cat_ID(single_cat_title('', false));
// Wordpress pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// WP_Query arguments
$args_news = array (
'post_type' => array( 'post' ),
'pagination' => true,
'posts_per_page' => '2',
'orderby' => 'date',
'paged' => $paged,
'cat' => $category_id,
);
// The Query
$query = new WP_Query( $args_news );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<?php get_template_part('categorytwo',get_post_format()); ?>
<?php }
} else {
// no news found
}
?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
?>
This pagination function works fine on my index.php page when I call it out.
<?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' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
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') ),
'prev_text' => __('« PREV'),
'next_text' => __('NEXT »'),
'total' => $wp_query->max_num_pages
) );
}
endif;
When I call it out on my index.php page it works fine.
<?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' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
Use this code on your loop:
<div class="posts_blog">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post', // Your post type name
'posts_per_page' => 5,
'paged' => $paged,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
?>
<div class="posts_container ">
// Posts format: <?php get_template_part('catalog',get_post_format()); ?>
</div>
<?php
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1) {
?>
<div class="pagination">
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%'.'/#posts-blog',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('<'),
'next_text' => __('>'),
));
?>
</div>
<?php
}
}
wp_reset_postdata();
if (function_exists("pagination")) {
pagination($wp_query->max_num_pages);
}
?>
</div>
Related
I have a custom post type 'charters' and within my category.php page I have a loop pulling in 'charter' posts for the current category id. I have a custom pagination setup displaying page numbers when I click page 2 link, it goes to URL /page/2 which displays a 404 error. I can't figure out why I am getting the 404. If I adjust my posts per page the pagination updates and displays the correct number of pages but the links do not show up.
Category.php Code:
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$product_args = array(
'post_type' => 'charters',
'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
'paged' => $paged,
'page' => $paged,
'cat' => $cat
);
$product_query = new WP_Query( $product_args ); ?>
<?php if ( $product_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
<article class="loop">
<h3><?php the_title(); ?></h3>
<div class="content">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists( 'custom_pagination' )) :
custom_pagination( $product_query->max_num_pages,"",$paged );
endif;
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Functions.php Code:
function prefix_change_cpt_archive_per_page( $query ) {
//* for cpt or any post type main archive
if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );
function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
My Permalink Settings are set to "Post Name" I have a feeling this has something to do with rewrites but I can not figure out how to get the page urls to work correctly. An example of my categories are as follows:
/category/long-beach/
/category/san-diego/
you should try this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = 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,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
you need to just replace url (client-testimonials) with your url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
If you are using default pagination then please use below code.
<?php
$current_page = get_queried_object();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'charters',
'paged' => $paged,
'cat'=> $cat);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
next_posts_link( 'Older Entries', $my_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_query();
?>
Add below code in your function.php
function posts_on_categorypage( $query ) {
if ( $query->is_category()) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'posts_on_categorypage' );
There was a couple of errors in my code. This is what I get for copy/pasting code and not going through it line by line. Thanks everyone who helped out, but I got this to work by changing:
Bad Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
Good Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( $cat ) ) {
$query->set( 'post_type', array( 'charters' ) );
$query->set( 'posts_per_page', '10' );
}
}
I have a seemingly simple WP query looking for posts on my site, and want these posts to be 'paged' accordingly. Below is my code, trying to achieve this, however all I am currently seeing is an empty .pager nav.
What is incorrect here? Help much appreciated.
Here is the live example.
PHP
<?php get_header(); ?>
<?php get_sidebar(); ?>
<main>
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'journal',
'posts_per_page' => 2,
'paged' => $paged,
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date'
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article>
/* Article content here */
</article>
<?php endwhile; ?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<nav class="pager">
<?php
$args = array(
'prev_next' => true,
'show_all' => true,
'type' => 'list'
);
echo paginate_links( $args );
?>
<ul class="page-prev-next">
<?php if( get_previous_posts_link() ) :
echo '<li class="prev-page">' . get_previous_posts_link( 'Prev' ) . '</li>';
endif; ?>
<?php if( get_next_posts_link() ) :
echo '<li class="next-page">' . get_next_posts_link( 'Next' ) . '</li>';
endif; ?>
</ul>
</nav>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
I'm trying to add some code to my wordpress theme to show a pagination at the bottom of the posts.
Here's my loop with the pagination:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 2 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- pagination -->
<?php
if($the_query->max_num_pages>1){?>
<p class="paged">
<?php
if ($paged > 1) { ?>
<
<?php }
for($i=1;$i<=$the_query->max_num_pages;$i++){?>
<a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
<?php
}
if($paged < $the_query->max_num_pages){?>
>
<?php } ?>
</p>
<?php } ?>
<!-- end pagination -->
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
When I'm looking through the source code I can't find the pagination. What am I doing wrong? Can't find an answer, no code works for me. Would be nice if someone could help me. :)
Use default WordPress pagination, here is an example:
<?php
// set the "paged" parameter
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// Pagination
echo get_next_posts_link( 'Older', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer' );
// clean up after our query
wp_reset_postdata();
else:
_e( 'Sorry, no posts matched your criteria.' );
endif;
To display next and previous links automatically, just use these functions:
next_posts_link();
previous_posts_link();
https://codex.wordpress.org/Pagination could help with this. In custom loops it helps to pass in the $max_pages variable as zero for unlimited pages, so it'd look like:
next_posts_link("Older Posts", 0);
If you want the pagination to include page links, in the format < 1 2 3 > rather than just next/previous links, you can use the WordPress function paginate_links.
So for your example the code would look like this:
<?php
//query
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
$args = array(
'posts_per_page' => 2,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//display your post here
}
//pagination
if($the_query->max_num_pages > 1){ ?>
<div class="paged">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
) );
?>
</div>
<?php }
} else {
echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';
}
wp_reset_postdata();
?>
So, I have the following to display post loops (wordpress):
METHOD A (works fine)
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts go here
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<nav id="rh_nav_below">
<ul>
<li class="rh_nav_previous"><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li class="rh_nav_next"><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
Now in the author page, following is used to display a post (a single posts):
METHOD B (works fine)
<?php rewind_posts(); while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I have been trying to change the author post (method b) to method A format, so I can control the number of posts, orderby and etc.
Here is what I have tried:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$rhp_author_profile_id = get_the_author_id();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $rhp_author_profile_id
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts show here.
However, I am only getting the admin's posts on every other authors.
What am I doing wrong?
Thanks
Try This
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
global $current_user;
get_currentuserinfo();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $current_user->ID
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
I have now this piece of code to show on each page 10 posts but know I want still the same but the maximum of posts in total can only be 30.
How do I do this?
<?php query_posts('showposts=10&paged='.$paged);?>
This is code where you want to display the post:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 10,'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post); ?>
<h1> <?php the_post_thumbnail(); ?></h1>
<h2><?php the_title(); ?></h2>
<h3><?php the_content(); ?></h3>
<?php endwhile; ?>
This is footer where you need to display pagination.
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if($loop->max_num_pages>1){?>
<ul class="pager">
<?php for($i=1;$i<=$loop->max_num_pages; $i++){ ?>
<li> <?php echo $i; ?></li>
<?php } ?>
</ul>
You should use wp_query to retrieve the posts, like Pieter Goosen said, is more efficient. Also wp_query have a property called $max_num_page that you can use to limit the number of pages you get.
You can modify your pagination to something like this:
global $wp_query;
if ( $wp_query->max_num_pages > 1 ){
$current_page = max( 1, get_query_var('paged') );
$max_pages = 3;
$args = array(
'base' => #add_query_arg('paged','%#%'),
'format' => '/paged/%#%',
'current' => $current_page,
'total' => $max_pages,
'show_all' => false,
'type' => 'array',
'paged' => 1
);
$pages = paginate_links( $args );
if (is_array($pages)) {
$paged = ( get_query_var('paged') == 0) ? 1 : get_query_var('paged');
foreach( $pages as $page) {
echo "$page";
}
}
}
I have also found this question. This code seems to work and you don't need to change any of your current code:
add_filter('pre_get_posts', 'limit_pages');
function limit_pages($query) {
$query->max_num_pages = 3;
if ($query->query_vars['paged'] > 3) {
$query->query_vars['paged'] = 3;
$query->query['paged'] = 3;
}
return $query;
}