Reputation: 2239
I have a select that allows a user to select the amount of posts shown on a blog listing page. At the moment the select auto-submits its form and the posts_per_page value is updated for the page query on re-load.
global $query_string;
if(isset($_REQUEST['set_posts_per_page']))
$ppp = $_REQUEST['set_posts_per_page'];
else
$ppp = 12;
query_posts("{$query_string}&posts_per_page=".$ppp);
Currently generating pagination buttons using standard WordPress functions:
previous_posts_link( '<span class="etc"></span>' );
next_posts_link('<span class="etc"></span>' );
What's the best way to re-work this so that users can use the pagination and posts_per_page options?
There are several pages like this listing different custom post types that have to work in the same way (if that makes a difference).
Upvotes: 0
Views: 1113
Reputation: 2239
This is what I have ended up doing. The only downside is that it now adds a get variable into the URL, but that's not the end fo the world.
Here I have replaced the next_posts_link() and previous_posts_link() completely, and instead added in custom bits of code. These get the URL that the pagination functions would usually get using get_previous_posts_page_link() and get_next_posts_page_link(), check for the set_posts_per_page REQUEST parameter and tack it onto the pagination URL.
I then build the nav buttons manually and input the URL with the get parameter. The strtok function stops this get parameter from stacking up in the URL by stripping it each time.
<?php global $wp_query;
$current_page = $wp_query->get( 'paged' );
/* Previous link */
$prev_url = strtok(get_previous_posts_page_link(), '?');
if($prev_url && $current_page!=1 && $_REQUEST['set_posts_per_page']!='')
$prev_url.="?set_posts_per_page=".$_REQUEST['set_posts_per_page'];
if($prev_url && $current_page!=1) : ?>
<a href="<?=$prev_url; ?>">
<span class="navbuttons__btn navbuttons__btn--next btn btn--ui" rel="next">
<i class="fa fa-arrow-left"></i>
</span>
</a>
<?php endif;
/* Next link */
$next_url = strtok(get_next_posts_page_link(), '?');
if($next_url && $current_page!=$wp_query->max_num_pages && $_REQUEST['set_posts_per_page']!='')
$next_url.="?set_posts_per_page=".$_REQUEST['set_posts_per_page'];
if($next_url && $current_page!=$wp_query->max_num_pages) : ?>
<a href="<?=$next_url; ?>">
<span class="navbuttons__btn navbuttons__btn--next btn btn--ui" rel="next">
<i class="fa fa-arrow-right"></i>
</span>
</a>
<?php endif; ?>
Upvotes: 1
Reputation: 447
<?php
// the query to set the posts per page to 5
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 5, 'paged' => $paged );
query_posts($args); ?>
<!-- the loop -->
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<!-- rest of the loop -->
<!-- the title, the content etc.. -->
<?php endwhile; ?>
<!-- pagination -->
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php else : ?>
<!-- No posts found -->
<?php endif; ?>
Upvotes: 0
Reputation: 447
Please find below code to fetch the blog.
<?php
$queryObject = new WP_Query( 'post_type=blog&posts_per_page=5' );
// The Loop!
if ($queryObject->have_posts()) {
?>
<ul>
<?php
while ($queryObject->have_posts()) {
$queryObject->the_post();
?>
also if you want add pagination on the page Add wp-pagination pluggin its to easy to use.
just add
<?php echo wp_paginate(); ?>
I hope this will help you.
Thank You
Upvotes: 0