I have now this piece of code to show on each page 10 posts but know I want still the same but the maximum of posts in total can only be 30.
How do I do this?
<?php query_posts('showposts=10&paged='.$paged);?>
This is code where you want to display the post:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 10,'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post); ?>
<h1> <?php the_post_thumbnail(); ?></h1>
<h2><?php the_title(); ?></h2>
<h3><?php the_content(); ?></h3>
<?php endwhile; ?>
This is footer where you need to display pagination.
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if($loop->max_num_pages>1){?>
<ul class="pager">
<?php for($i=1;$i<=$loop->max_num_pages; $i++){ ?>
<li> <?php echo $i; ?></li>
<?php } ?>
</ul>
You should use wp_query to retrieve the posts, like Pieter Goosen said, is more efficient. Also wp_query have a property called $max_num_page that you can use to limit the number of pages you get.
You can modify your pagination to something like this:
global $wp_query;
if ( $wp_query->max_num_pages > 1 ){
$current_page = max( 1, get_query_var('paged') );
$max_pages = 3;
$args = array(
'base' => #add_query_arg('paged','%#%'),
'format' => '/paged/%#%',
'current' => $current_page,
'total' => $max_pages,
'show_all' => false,
'type' => 'array',
'paged' => 1
);
$pages = paginate_links( $args );
if (is_array($pages)) {
$paged = ( get_query_var('paged') == 0) ? 1 : get_query_var('paged');
foreach( $pages as $page) {
echo "$page";
}
}
}
I have also found this question. This code seems to work and you don't need to change any of your current code:
add_filter('pre_get_posts', 'limit_pages');
function limit_pages($query) {
$query->max_num_pages = 3;
if ($query->query_vars['paged'] > 3) {
$query->query_vars['paged'] = 3;
$query->query['paged'] = 3;
}
return $query;
}
Related
I have been trying to add numeric pagination to my posts page which are filtered by category. The problem is that pagination won't appear when I call it. Pagination works perfectly fine when I use previous_posts_link and next_posts_link but this is not excatly what I want to achieve. What am I missing here?
My filtered category page.
<?php
// Get post ID
$post_type = get_post_type( $post->ID );
// Get category ID
$category_id = get_cat_ID(single_cat_title('', false));
// Wordpress pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// WP_Query arguments
$args_news = array (
'post_type' => array( 'post' ),
'pagination' => true,
'posts_per_page' => '2',
'orderby' => 'date',
'paged' => $paged,
'cat' => $category_id,
);
// The Query
$query = new WP_Query( $args_news );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<?php get_template_part('categorytwo',get_post_format()); ?>
<?php }
} else {
// no news found
}
?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
?>
This pagination function works fine on my index.php page when I call it out.
<?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(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
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') ),
'prev_text' => __('« PREV'),
'next_text' => __('NEXT »'),
'total' => $wp_query->max_num_pages
) );
}
endif;
When I call it out on my index.php page it works fine.
<?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(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
Use this code on your loop:
<div class="posts_blog">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post', // Your post type name
'posts_per_page' => 5,
'paged' => $paged,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
?>
<div class="posts_container ">
// Posts format: <?php get_template_part('catalog',get_post_format()); ?>
</div>
<?php
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1) {
?>
<div class="pagination">
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%'.'/#posts-blog',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('<'),
'next_text' => __('>'),
));
?>
</div>
<?php
}
}
wp_reset_postdata();
if (function_exists("pagination")) {
pagination($wp_query->max_num_pages);
}
?>
</div>
I'm trying to add a custom query to a WordPress template and include pagination but my pagination isn't appearing, for example's sake I'm trying to add this to page.php.
I have the following markup which works perfectly when place inside a category template like category.php, the pagination shows up and functions just fine. The issue is that the pagination doesn't appear when the same code is place in page.php or any custom page template.
The query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'desc',
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop Markup goes here.
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php pagination(); ?>
Pagination() as defined in functions.php:
function pagination() {
global $wp_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
}
add_action('init', 'pagination');
I've seen a few posts requesting help on the same subject, but I didn't come across an elegant solution.
Any advice would be much appreciated! My knowledge of PHP is pretty limited, I took the pagination() function from the HTML5 Blank theme by Todd Motto so I don't 100% understand what that function defines.
Managed to find a solution by merging my code from the original post with the following code from this tutorial:
I'm posting a complete example of a simple page.php for anyone lost and needing more context to implement this, this works perfectly for me with no broken aspects like each page number returning the same posts or anything.
<?php get_header(); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="the_loop">
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'orderby' => 'desc',
'orderby' => 'date' // modified | title | name | ID | rand
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop code goes here.
<?php endwhile; ?>
<?php if ($loop->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $loop;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_template_part('footer'); ?>
So, I have the following to display post loops (wordpress):
METHOD A (works fine)
<?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(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts go here
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<nav id="rh_nav_below">
<ul>
<li class="rh_nav_previous"><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li class="rh_nav_next"><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
Now in the author page, following is used to display a post (a single posts):
METHOD B (works fine)
<?php rewind_posts(); while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I have been trying to change the author post (method b) to method A format, so I can control the number of posts, orderby and etc.
Here is what I have tried:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$rhp_author_profile_id = get_the_author_id();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $rhp_author_profile_id
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts show here.
However, I am only getting the admin's posts on every other authors.
What am I doing wrong?
Thanks
Try This
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
global $current_user;
get_currentuserinfo();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $current_user->ID
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
I have a problem with WP Pagination.
Previously, my pagination worked on taxonomy pages and was displaying same result on page 2 than page 1 on Static Homepage.
I found a solution to this problem, and 3 days ago all my page worked perfectly.
Until yesterday : now pagination still works on taxonomy pages, but on my static Homepage, page 1 works, but page 2 and more return "No article in this category" like if wp_query loop don't find results.
No file has been edited, functions are the same than 3 days ago.
PS : If I remove correction of first bug (Page 2 shows the same than page 1), the bug don't come back, homepage page 2 still returns "No article in this category";
Here is my code :
// Define $post_type in previous included file, already checked if parameter correctly retrieved
$post_type = array('magicrecipe', 'post', 'yoga-article', 'feelgood', 'gastronomie', 'voyage');
<ul class="articlesList row mw">
<?php
$taxQuery = array('taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $categoryValue);
// "Page 2 show the same than page 1" fix
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
if ($search) {
$args = array(
'posts_per_page' => 12,
'paged'=>$paged,
'orderby'=> 'date'
);
$args['s'] = $search;
}else{
$args = array(
'post_type' => $post_type,
'posts_per_page' => 12,
'orderby'=> 'date',
'paged'=> $paged,
'tax_query' => array(array($taxQuery))
);
}
$loop = new WP_Query($args);
$count = $loop->post_count;
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $loop;
if ($count == 0) {
echo '<p class="articles-noArticle">Il n\'y a pas encore d\'article dans cette catégorie !</p>';
} else {
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<li class="articlesList articles-article col-1-4 col-m-1-2 col-s-1-1">
<a href="articles-link">
<div class="articles-thumbContainer js-squareThumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(array(500, 500), array('class' => 'articles-thumb')); ?>
</a>
</div>
<h3 class="article-title"><?php the_title(); ?></h3>
<p class="article-excerpt"><?php the_excerpt(); ?></p>
En savoir plus
</a>
</li>
<?php
endwhile;
}
</ul>
<div class="pagination">
<?php
// Custom query loop pagination
echo paginate_links();
// wp_reset_query();
?>
</div>
Thanks for your help, I search everywhere but found anything about this problem.
You have too many conditions.
<?php
//You don't need that
/*if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }*/
//Try this:
<?php
wp_reset_query();
$paged = get_query_var('paged', 1);
$args = array(
'post_type' => $post_type,
'posts_per_page' => '12',
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'post-date'
);
$loop = new WP_Query( $args);
?>
How can I adapt this code to limit the number of posts to say 3 on the page? It's taken from a Wordpress template. Thanks in advance for any help!
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$alt_args = array(
'ignore_sticky_posts' => 1,
'paged' => $paged
);
$alt_posts = new WP_Query($alt_args);
?>
<?php if ( $alt_posts->have_posts() ) : ?>
<?php while ( $alt_posts->have_posts() ) : $alt_posts->the_post(); ?>
<?php get_template_part( 'content', 'alt-homepage' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'wp-jurist' ); ?></h1>
set the posts_per_page parameter:
$alt_args = array(
'ignore_sticky_posts' => 1,
'paged' => $paged,
'posts_per_page' => 3
);
see http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters for details
While is not the right thing to do, this will work:
<?php
$i = 0;
while ( $alt_posts->have_posts() && $i < 3 ) {
$i++;
$alt_posts->the_post();
get_template_part( 'content', 'alt-homepage' );
} ?>
The code above will limit the maximum of proccessed elements to 3. Just change the while part, and it'll run. Anyway, the correctness would be that the have_post() method would have an option to limit the number of elements retrieved, and then only show that elements.