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(); ?>
Related
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>
I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);
I've construced two Wordpress loops with numeric pagination as follows:
<div class="container" style="background:#ccc">
<?
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => false,
'category_name' => 'latest',
'posts_per_page' => 1,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
?>
<!-- second -->
<?
$args2 = array(
'paged' => $paged2,
'category_name' => 'uncategorized',
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
<!-- container -->
Everything works fine, but the paginatin page also shows the 'latest' post loop, although I've set the pagination to false.
How could I hide the first loop on the pagination pages?
Advice appreciated.
use wp_reset_postdata() function after your loop end
follow this code, may be it'll help you
<?php
// example args
$args = array( 'posts_per_page' => 3 );
// the query
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?><!-- end of the loop -->
<!-- put pagination functions here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
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();
?>
I am using this shortcode to show products form a category on a page . but when we are using this then it is not showing pagination . because we have so many products in our category .
we are using the following code:
[product_category category="snowpeak" per_page="12" columns="4" orderby="date" order="desc"]
You can vote for this feature to implemented into the WooCommerce Shortcode here:
http://ideas.woothemes.com/forums/133476-woocommerce/suggestions/4146798-add-pagination-support-for-list-of-products-render
Otherwise you can create a custom Product Loop (http://docs.woothemes.com/document/sample-products-loop/) and add a custom arg in WP_Query for Pagination:
eg.
<ul class="products">
<?php
global $paged;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_postdata(); ?>
</ul><!--/.products-->
reference: pagination on custom post wp_query
You can use same pagination in product page which you used in Post page.
For an instance
Add the following code in functions.php file
function custom_pagination() {
global $wp_query;
$big = 999999999;
$pages = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?page=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '← Previous',
'next_text' => 'Next →',
));
if (is_array($pages)) {
$current_page = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination">';
foreach ($pages as $i => $page) {
if ($current_page == 1 && $i == 0) {
echo "<li class='active'>$page</li>";
} else {
if ($current_page != 1 && $current_page == $i) {
echo "<li class='active'>$page</li>";
} else {
echo "<li>$page</li>";
}
}
}
echo '</ul>';
}
}
Next Step is to call the function, in post or product page for do the pagination job
For an example of wp_query
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query("category_name='prayer'&paged=$paged&posts_per_page=12");
?>
<?php if ( $wp_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php get_template_part('prayer-loop'); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<div class="row" style="clear: both;">
<?php custom_pagination(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
This pagination will work greatly if you add bootstrap to your website.
You can try this.
[products per_page="4" columns="2" paginate="true"]
<ul class="products">
<?php
global $paged;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_postdata(); ?>
</ul><!--/.products-->