Can a Wordpress expert confirm - is there a simple, elegant, Wordpress-native way in which get_posts() queries can be paginated? I have tried to piece together how to do it from various posts, but can't seem to get it to work.
I often use get_posts() for querying my Custom Post types - but if I can't easily paginate these I may just drop get_posts() and get into the habit of using wp_query() for Custom POst Types.
What is wrong with my code? I have struggled through many PHP problems, but would still consider myself a novice.
Trying for the simplest clearest option here so I can understand how it works, use it as a template for future projects.
<div class="content-primary">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query_args = array(
'post_type' => 'project',
'posts_per_page' => 10,
'paged' => $paged
);
$query_custom_posts = get_posts( $query_args );
foreach ( $query_custom_posts as $post ) : setup_postdata( $post ); ?>
<article>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : the_post_thumbnail( 'thumbnail' ); endif;?>
<h3><?php the_title(); ?></h3>
</a>
<div><?php the_date(); ?></div>
<div><?php the_excerpt(); ?></div>
</article>
<?php endforeach; ?>
<?php
global $query_args;
$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' => $query_args->max_num_pages
) );
?>
<?php wp_reset_postdata();?>
</div>
I really hoped this would work, though I am aware of errors in it - I know my $query_args contains no max_num_pages.
Would be nice to have the optional use of the Wordpress next_posts_link() / previous_posts_link() too, rather than paginate_links(), but not sure if this can be made to work with get_posts().
You simply need to add the following line:
<?php query_posts('post_type=post&paged='. get_query_var('paged')); ?>
just before your get_posts() loop starts.
Full article here: http://blog.onireon.it/wordpress-get_posts-and-pagination-posts-are-correctly-displayed-but-no-pagination-links-are-showing/
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 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 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>