I am trying to build a paginated archive sidebar using WP_Query. As the same sidebar is used on both single.php and news.php, the arguments used for the WP_Query are different.
On news.php, the pagination works perfectly, but on single.php the pagination links are present, and have the correct href values, but simply reload the page when clicked, i.e. anything beyond page 1 is inaccessible.
Update: Just in case this is relevant, single.php makes use of the Share Buttons by AddToAny, and Social (by MailChimp) plugins. I have deactivated both, but this has not fixed the issue with pagination.
Can anyone shed some light on this for me? Code here:
<aside>
<header>
<h1 class="column-title">Archive</h1>
<hr>
</header>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// remove first four posts if this is the news main page
if ( ! is_single() ) {
// Save first four posts
$first_four = new WP_Query ('post_type=post&orderby=date&order=desc&posts_per_page=4');
if ( $first_four->have_posts() ) : while ( $first_four->have_posts() ) : $first_four->the_post();
$skipIDs[] = $post->ID;
endwhile; endif;
wp_reset_postdata();
// Save all posts
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1
);
$all_posts = new WP_Query($args);
while ( $all_posts->have_posts() ) : $all_posts->the_post();
// Skip first four posts and save rest
if ( in_array($post->ID,$skipIDs) ) { continue; };
$offset_array[] = $post->ID;
endwhile;
wp_reset_postdata();
// Final arguments for WP_Query
$args = array(
'post__in' => $offset_array,
'paged' => $paged,
'posts_per_page' => 5
);
} else {
// Args for single.php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
}
$article_archive = new WP_Query($args);
$max_pages = $article_archive->max_num_pages;
if( $article_archive->have_posts() ) : while( $article_archive->have_posts() ) : $article_archive->the_post();
$has_image = get_the_post_thumbnail($post->ID,'thumbnail'); ?>
<article class="group">
<a class="anchor-overlay" href="<?php the_permalink(); ?>"></a>
<?php if( $has_image ) { echo $has_image; } else { echo "<img src='/wp-content/themes/DCSFC/images/news-calendar/news/no-image.jpg' alt='No image' />"; } ?>
<div>
<h3><?php the_title(); ?></h3>
<time datetime="dd/MM/YYYY"><?php echo get_the_date(); ?></time>
</div>
</article>
<?php endwhile; endif;
wp_reset_postdata();
$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' => $max_pages
) );
?>
</div>
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 am facing a little issue while adding pagination to a custom post type which I've created inside Wordpress. The pagination links are appearing on the template where I've all the posts, inside the custom post type, listed but when I click on the link to view Older posts, it opens the second page but the same posts from the first page are displayed there. Moreover, on the second page, the 'Older posts' link doesn't update to '../page/3', instead it stays '../page/2'. I followed the steps specified here (https://stackoverflow.com/a/18325002/2115001) and modified my code according to the information listed under 'Option 2'. Here's what my code currently looks like:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=medals'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
echo '<nav>';
echo previous_posts_link('« Newer');
echo next_posts_link('Older »');
echo '</nav>';
$wp_query = null;
$wp_query = $temp; // Reset
?>
Do I need to add some code to the functions.php file in order for this to function properly or there's something wrong in my original code? Thanks.
Try below code
$paged = get_query_var('page') ? get_query_var('page') : 1;
$args = array('posts_per_page' => 3,
'paged'=> $paged,
'post_type' => 'medals');
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
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,
'next_text' => __('Next »'),
));
Try this:
<?php
$type = 'portfolio';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign original query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query();
wp_query->query($args);
?>
then put your permalinks to the default structure and back to how the structure was.
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article class="">
<div class="">
<h4><?php the_time( 'm' ); ?></h4>
<h4><?php the_time( 'd' ); ?></h4>
</div>
<div class="">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</article>
<?php endwhile; // End the loop. Whew. ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
//echo esc_url( get_pagenum_link());
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'type'=>'list',
'prev_text' => __('<'),
'next_text' => __('>'),
) );
?>
Hope it will work for you.
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.
I'm setting up a blog listings template for a site with multiple parent pages, and I need this page template to be used across the site, various pages will require there own blog listings page. Calling relevant posts depending on the Parent category.
i.e:
Food (Site Homepage)
Food Blog (Listings page for all Food related posts)
Fruit (Parent page)
Fruit Blog (Listings page for all Fruit related posts)
Veg (Parent page)
Veg Blog (Listings page for all Veg related posts)
My problem is that the posts per correct parent category aren't being called. I'm getting all posts.
I've set my code up like this: Many thanks in advance.
<div id="bloglistings">
<?php wp_reset_query(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$current_cat = intval( get_query_var('cat') );
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : ?>
<div class="row">
<?php $count=0; ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="span6">
<div class="media feature one">
<a class="pull-left" href="<?php the_permalink(); ?>">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail', array('class' => 'img-circle')); ?>
<img class="hoverimage" src="<?php echo get_template_directory_uri(); ?>/img/icon-read-bloglistings.png" alt="">
</a>
<div class="media-body">
<h2><?php the_title(); ?></h2>
<p class="date"><?php the_time('F j, Y'); ?></p>
<p><?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
</div>
</div>
</div>
<?php $count++; ?>
<?php if ($count==2 ||$wp_query->found_posts==0) :
echo '</div><div class="row">';
?>
<?php $count=0; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<h2>Sorry but there are no posts.</h3>
<?php endif; ?>
<!-- PAGINATION -->
<div class="pagination">
<ul>
<li>
<?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' => $wp_query->max_num_pages,
) );
?>
</li>
</ul>
</div>
<?php wp_reset_query(); ?>
</div><!-- /.row -->
</div><!--/bloglistings-->
Try adding the category to the query.
Replace this:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
with
$args = array(
'post_type' => 'post',
'category__in' => array( $current_cat ),// category was missing here
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
I have the following code:
$attr = array(
'align' => 'left',
'class' => 'thumbnail imageRight',
'width' => 350,
'height' => 350
);
$post_query = array ( 'post_type' => 'post' );
$posts = new WP_Query ( $post_query );
if($posts->have_posts()){
while($posts->have_posts()){
$posts->the_post();
?>
<div class="post">
<?php the_post_thumbnail('medium', $attr); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
</div>
<?php
}
next_posts_link('« Older Entries');
previous_posts_link('Newer Entries »');
}
Which can be seen in action here. This page displays currently 3 of the 21 posts in the database. How ever there is no pagination.
Can some one tell me why?
Both next_posts_link and previous_posts_link use the global $wp_query and $paged. You have to chase function calls around the source code to see that (but it is pretty obvious with next_post_links). They don't work with custom queries but I believe you can cheat.
$old_wpq = $wp_query;
$wp_query = new WP_Query ( $post_query );
// your loop
$wp_query = $old_wpq;
Try that.
There is a related thread wordpress.stackexchange.com.
https://wordpress.stackexchange.com/questions/77661/next-posts-link-works-only-with-original-wp-query/77666#77666
<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post', //Post type
'posts_per_page' => 3, //How many post u want to display per page
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
<?php } } ?>
<div class="pagination">
<?php
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
) );
?>
</div>