PRECONDITION:
I created custom post type "comparison"
I created 'widget.php' and registered it in 'functions.php'
I added widget to 'front-page.php'
TASK:
On my Front Page and I want to list all posts with type "comparison" in separate widget with pagination
PROBLEM:
I don't know the 'best practice', how to implement it technically. How to list all "comparison" in widget?
Here is the custom post template code, You can assign the template on the page you want to display the posts list. You can try with below template code:
<?php
# Template Name: Comparison List
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 15,
'paged' => $paged,
'post_type' => 'comparison',
'orderby' => 'title',
'order' => 'asc'
);
$wp_query = new WP_Query($args);
?>
<?php get_header(); ?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'template-parts/content', 'comparison-list-item' ); ?>
<?php endwhile; ?>
<?php print_pagination($wp_query); ?>
<?php else: ?>
<?php get_template_part( 'template-parts/content', 'nothing-found' ); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
Total noob here. I checked all over the net but I'm finding it difficult to add the pagination feature to my homepage. I'm using the recent_products per_page
query on my homepage but I want to limit to 12 products so that I can add the infinity scroll option.
<?php get_header(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => -1
);
$wp_query = new WP_Query($args);
if (isset($_GET['all']))
{
?>
<?php do_action('woocommerce_archive_description'); ?>
<?php if (have_posts()) : ?>
<?php
// I don't want the sorting anymore
//do_action('woocommerce_before_shop_loop');
?>
<ul class = "products-list">
<?php while (have_posts()) : the_post(); ?>
<?php woocommerce_get_template_part('content', 'product'); ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php
/* woocommerce pagination */
do_action('woocommerce_after_shop_loop');
?>
<?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>
<?php woocommerce_get_template('loop/no-products-found.php'); ?>
<?php endif; ?>
<?php else: ?>
{
// Code to display the product categories with thumbnails.
}
?>
<?php get_footer(); ?>
Would be great if someone can help me out with this. I need this template or a custom template you suggest to show the same pagination as the shop page (1,2,3) etc..
Thanks.
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!
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?
I've ran into a problem where I can't seem to get pagination to work on the latest wordpress version 3.4.2. I have developed a lot of wordpress sites so this rather confusing. I've got a custom post type of news, I wish to show 8 posts per page and the page url would be '/news'. This page displays correctly with the right pagination at the bottom (using WP-pagenavi). When I click on the second page (/news/page/2) I get my 404 page. I'd really appreciate any help on resolving this.
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div class="container_12">
<div class="grid_12 box">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'news', 'posts_per_page' => 8, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="news">
<h4><?php the_title() ?></h4></a>
<span><?php the_time('F jS, Y'); ?></span>
<p><?php limit_excerpt("30"); ?></p>
</div>
<?php endwhile; ?>
<div id="pagination">
<?php if(function_exists('wp_pagenavi')) {
wp_pagenavi( array(
'query' =>$loop
));
}
?>
</div>
</div>
</div><!-- .container_12 -->
<?php get_footer(); ?>
[Update - Solution]
So I've coded a solution for this. Put this in the functions.php
//Set for cpts
function cpt_init( $cpt )
{
add_rewrite_rule(
"{$cpt}/page/([^/]+)/?",
"index.php?pagename={$cpt}&pag=$matches[1]",
'top' );
}
//Only Add Once
add_filter( 'query_vars', 'cpt_query_vars' );
function cpt_query_vars( $query_vars )
{
$query_vars[] = 'pag';
return $query_vars;
}
//Add for each type
add_action( 'init', 'cpt_init', 'news' );
And in your loop use get_query_var('pag') instead of get_query_var('page')
Did you try using global variable $paged instead of attributing it your own value ?
This is for example what is used in a cutom theme :
<?php
global $paged;
$the_query = new WP_Query();
$the_query->query('post_type=post&posts_per_page=14&post_status=publish&paged='.$paged);
?>