After dedicating hours on this issue I finally decided to ask the community about the problem I am facing. This issue itself is somewhat interesting and strange.
I created a custom taxonomy template and while I am using the default wordpress loop I am facing issues in Pagination. The posts in every page are coming randomly.
So lets say if I am on page 1 of the taxonomy pagination it shows me 2 posts and if I go to page 2 it shows me 4 posts then 2 and then 3 posts per page ..
I have set the posts_per_page as 6 using pre_get_posts action on my functions.php file ..
I am giving the brief codes below ..
TEMPLATE FILE
<?php get_header(); ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
?>
<div <?php post_class('main-section page-inner-wrapper'); ?>>
<?php get_template_part('talent_filter', 'talent_filter.php'); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- POST HTML -->
<?php endwhile; ?>
<!-- PAGINATION BEGIN -->
<nav class="navigation pagination" role="navigation">
<?php
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $paged,
'prev_text' => __( '<i class="fa fa-angle-left"></i>', 'bridge' ),
'next_text' => __( '<i class="fa fa-angle-right"></i>', 'bridge' ),
'before_page_number' => '<span class="meta-nav">' . __( '', 'bridge' ) . ' </span>',
'total' => $wp_query->max_num_pages
) );
?>
</nav>
<!-- PAGINATION END -->
<?php endif; ?>
</div>
<?php get_footer(); ?>
FUNCTIONS IN function.php
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if( is_tax( 'talent_groups' ) ) {
$query->set('posts_per_page', 6);
$query->set('posts_per_archive_page', 6);
$query->set('orderby', 'date');
}
}
}
add_action( 'pre_get_posts', 'my_post_queries');
// Solves Pagination Pages 404's
add_filter( 'option_posts_per_page', 'tdd_tax_filter_posts_per_page' );
function tdd_tax_filter_posts_per_page( $value ) {
return (is_tax('talent_groups')) ? 1 : $value;
}
Related
I am trying to figure out a way to make the_posts_pagination or any alternative output numeric post navigation for a custom query.
but it doesn't seem to work, I don't know if I am doing anything wrong would appreciate suggestions and solutions thanks in advance
My Code
<?php global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&order=ASC'); ?>
<div class="main-post-loop">
<div class="big-thum-section img-is-responsive">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('small-block-thumb'); ?>
<?php endif; ?>
</div>
<div class="squiggle-post-meta-section clearfix">
<h2> <?php the_title(); ?> </h2>
<div class="excerpt-post"><?php the_excerpt(); ?></div>
</div>
<div class="continue-reading-section">
Continue reading <i class="fa fa-chevron-right"></i>
</div>
<div class="squiggly-line"></div>
</div>
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => esc_html( '←' ),
'next_text' => esc_html( '→' ),
) );
?>
<?php wp_reset_query(); // reset the query ?>
In order to do that there are a few steps you need to do, first of all i suggest you use the wp-pagenavi plugin, it handles lots of things for you.
Any way, i explain both ways, with and without the plugin,
first we write our query and set the paged attribute according to paged query var, so when the user navigates to for example page 3, the query filters posts and shows the third page posts:
$paged = (int) ( get_query_var( 'paged' ) ?: ( get_query_var( 'page' )?: 1 ) );
$my_query = new WP_Query( array(
'posts_per_page' => 10,
'paged' => $paged // This is important for pagination links to work
) );
Now if you have decided to use the wp-pagenavi plugin, it's quite easy to make paginations with custom queries, all you need to do is :
<?php if( function_exists('wp_pagenavi') ) wp_pagenavi( array( 'query' => $my_query ) ); ?>
But if you wish to use the_posts_pagination() function, i'm not sure if it supports custom queries, but since it's using the paginate_links() function , it should work with this arguments
$args = array(
'current' => max( 1, $paged ), // $paged is what we defined earlier or you can use just get_query_var('paged')
'total' => $my_query->max_num_pages
)
if it doesn't, you can use the paginate_links() function itself with the same above arguments.
See also : this answer
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('post_type' => 'post', 'order' => 'ASC', 'paged' => $paged, 'posts_per_page' => 12 ));
if( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<div class="pagination"><?php my_pagination(); ?></div>
<?php endif; ?>
<?php wp_reset_query(); ?>
In your functions.php add,
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;
OR
Try this: http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
I have this code which works, showing "Older Entries" and "Newer Entries". However, I wanted to edit this code to show the "1", "2", "3", and so on without left/right arrows or Prev/Nex text. Therefore, I did research and was confused. I can understand what the tutorials such as Wordpress Pagination (Numbered pagination is at bottom), How To Add Pagination To Your WordPress Theme, and Paginate Links, but they didn't say anything about where to put the code in. Only the second tutorial explained in full what I need to do but even so, that way wasn't working...
My code that works:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=9&posts_per_page=9&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="proyectpost">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="innerpost">
<div class="postthumbnail">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
} ?>
</div>
<div class="posttitle">
<h2><?php the_title(); ?></h2>
</div><!-- .entry-header -->
<div class="postsubtitle">
<div class="datepanel">
</div>
</div>
</div>
</article><!-- #post-## -->
</div>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The second tutorial said to add this code to the functions.php:
// Numbered Pagination
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
and to replace the default pagination with this code
<?php wpex_pagination(); ?>
How do I get it to work to display the numbered pagination in my wordpress template php file?
After researching a bit further, I found some clues which gave me inspiration to figure out where to put the right code so that I can get numbered pagination.
You use this code:
<?php
$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' => $the_query->max_num_pages
) );
?>
to replace:
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
I think what makes me confused was when I was researching, some said to add some code in the functions.php while others said to use other code in certain places. Therefore, when I was reading paginate links, this page wasn't clear on whether or not this new code above should be added to functions.php or to replace "prev/next" links.
Anyway, it works now! :)
I have a static frontpage on my wordpress website, displaying my posts. Currently it shows 10 posts and I want to make it possible to click "next page" to see the next 10 posts in the query and so on. I have tried getting the pagination to work, but it looks like I'm unable to connect it with my search query.
Here is my query:
<?php
query_posts(
array( 'post_type' => 'post',
'order' => 'DESC',
'meta_key' => 'cf_votes',
'orderby' => 'meta_value_num',
'posts_per_page' => 10
)
);
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
And here is the pagination I have tried:
<?php
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
) );
?>
So basically I'm able to see the first 10 posts perfectly. But no alternative to see more posts is shown.
Wordpress Codex is pretty comprehensive on the Pagination problem, I recommend you to see this:
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
There's a section specifically for the Static Front Page.
Something like:
<?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(
'posts_per_page' => 3,
'paged' => $paged
);
query_posts($args);
?>
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
i faced very weird issue. I make a post type "insight" and now i want a pagination (No Plugin). when i click on next post it shows me 404 error
here is code
<?php
if ( get_query_var('paged') ){
$paged = get_query_var('paged');
}
else {
$paged = 1;
}
query_posts(array('post_type'=>'insight','posts_per_page'=>'1','order'=>'DESC', 'paged' => $paged)); ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« New Entries') ?></div>
<div class="alignright"><?php previous_posts_link('« Older Entries') ?></div>
</div>
I don't no why this happen because i think my code right.
Foy my blog I use this:
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;
and then after the endwhile, in the page that I want to use the pagination like categories, index etc... I use:
<?php endwhile; ?>
<?php my_pagination(); ?>
I hope this helps if you try it :)
Edit - to see it in action visit: http://ios-blog.co.uk
I've been looking at this problem for a few weeks now, constantly running into the same problem. It's a bit of a complicated setup:
I've got a category called "dossiers". I have also got a custom taxonomy "dossiers" which is used to group posts that are about the same news event or story.
When someone creates a post, they can assign the post to certain dossier and then put it in the category for "dossiers". That way, all dossier-posts are filed under the same category while still having a way to separate stories from each other.
I need the category page for the category "dossiers" to display all the custom taxonomy terms in the taxonomy "dossiers". These, in turn, link to their own listing of that particular dossier. I created the category-dossiers.php file which replaces the default category page, so that's not a problem anymore.
Using other forum posts, particular this one, I have gotten the bulk of the problem figured out. The category-page shows all taxonomy terms, with links and everything, and can split them up into pages. (This is a bit hacky because WordPress normally doesn't use terms as the main content blocks, but posts).
My only problem right now is to get the second, third, etc. page working. Beyond the first page it just displays the default index.php file of my theme.
Here is the full template page. Can anyone figure out what's wrong with it? I've tried and tried but just can't seem to find the problem.
<?php get_header(); ?>
<?php get_sidebar (); ?>
<section class="articles grid main-content category">
<header class="grid-header">
<h2><?php single_cat_title();?></h2>
</header>
<?php
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var('paged');
}
else if ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
}
else {
$paged = 1;
}
$per_page = 20;
$number_of_terms = count( get_terms( "dossiers") );
$offset = $per_page * ( $paged - 1) ;
// Setup the arguments to pass in
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'offset' => $offset,
'number' => $per_page
);
// Gather the terms
$terms = get_terms("dossiers", $args);
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {?>
<li class="article">
<a href="<?php echo get_term_link( $term ); ?>">
<?
if ( get_field('banner', $term->taxonomy._.$term->term_id) ) { // check if the post has a Banner Image assigned to it.
$image = get_field('banner', $term->taxonomy._.$term->term_id);
echo '<img class="wp-post-image" src="'.$image[sizes]['thumbnail'].'" />';
}
else {
echo '<img class="wp-post-image" src="' . get_bloginfo( 'stylesheet_directory' ) . '/img/missing-thumbnail.png" />';
}
?>
<div class="meta-info" />
<div class="caption" data-category="<?php $category = get_the_category(); echo $category[0]->slug; ?>"><?php echo $term->name; ?> </div>
</div>
</a>
</li>
<?}
echo "</ul>";
}
?>
</section>
<nav class="pagination">
<?php
if($terms) {
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => $paged,
'total' => ceil( $number_of_terms / $per_page ), // 3 items per page
'type' => 'list',
'mid_size' => 5
) );
}
?>
</nav>
<?php get_footer(); ?>
change the query array
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'offset' => $offset,
'paged' => $per_page
);
instead of number used paged.