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.
Related
I have a seemingly simple WP query looking for posts on my site, and want these posts to be 'paged' accordingly. Below is my code, trying to achieve this, however all I am currently seeing is an empty .pager nav.
What is incorrect here? Help much appreciated.
Here is the live example.
PHP
<?php get_header(); ?>
<?php get_sidebar(); ?>
<main>
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'journal',
'posts_per_page' => 2,
'paged' => $paged,
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date'
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article>
/* Article content here */
</article>
<?php endwhile; ?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<nav class="pager">
<?php
$args = array(
'prev_next' => true,
'show_all' => true,
'type' => 'list'
);
echo paginate_links( $args );
?>
<ul class="page-prev-next">
<?php if( get_previous_posts_link() ) :
echo '<li class="prev-page">' . get_previous_posts_link( 'Prev' ) . '</li>';
endif; ?>
<?php if( get_next_posts_link() ) :
echo '<li class="next-page">' . get_next_posts_link( 'Next' ) . '</li>';
endif; ?>
</ul>
</nav>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
I'm trying to add some code to my wordpress theme to show a pagination at the bottom of the posts.
Here's my loop with the pagination:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 2 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- pagination -->
<?php
if($the_query->max_num_pages>1){?>
<p class="paged">
<?php
if ($paged > 1) { ?>
<
<?php }
for($i=1;$i<=$the_query->max_num_pages;$i++){?>
<a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
<?php
}
if($paged < $the_query->max_num_pages){?>
>
<?php } ?>
</p>
<?php } ?>
<!-- end pagination -->
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
When I'm looking through the source code I can't find the pagination. What am I doing wrong? Can't find an answer, no code works for me. Would be nice if someone could help me. :)
Use default WordPress pagination, here is an example:
<?php
// set the "paged" parameter
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// Pagination
echo get_next_posts_link( 'Older', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer' );
// clean up after our query
wp_reset_postdata();
else:
_e( 'Sorry, no posts matched your criteria.' );
endif;
To display next and previous links automatically, just use these functions:
next_posts_link();
previous_posts_link();
https://codex.wordpress.org/Pagination could help with this. In custom loops it helps to pass in the $max_pages variable as zero for unlimited pages, so it'd look like:
next_posts_link("Older Posts", 0);
If you want the pagination to include page links, in the format < 1 2 3 > rather than just next/previous links, you can use the WordPress function paginate_links.
So for your example the code would look like this:
<?php
//query
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
$args = array(
'posts_per_page' => 2,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//display your post here
}
//pagination
if($the_query->max_num_pages > 1){ ?>
<div class="paged">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
) );
?>
</div>
<?php }
} else {
echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';
}
wp_reset_postdata();
?>
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;
}
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 using 2 custom queries to load posts on a custom Wordpress template homepage.
the first query loads 4 'sticky' posts, and the next query loads several more recent posts.
This works great and does what I need it to do, but the pagination links when clicked show the same content on every page.
For example I have 10 posts on the homepage, and when i click to page 2 - the exact same 10 posts appear again. If I click pagination to visit page 3, the same 10 posts again etc.
No matter what page I am on, the same 10 posts are listed.
I have read something about paged being required but several failed attempts to include that in my below code have broguht me here.
Any ideas appreciated!
My code...
<?php $sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<main id="main" class="site-main" role="main">
<?php $the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
You sure need the $paged parameter (docs). Untested but give it a go:
<?php
// $paged holds the current page offset
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
/**
* - Checks if the current page is 'paged' (false on first page)
* - Remove the check if you need those sticky posts on all pages
* - I added the $paged parameter so those sticky posts will paginate
* if you decide to show them on all pages
*
*/
if( ! is_paged() ) { // Sticky posts only on first page
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
'paged' => $paged // ah, the page offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<?php the_title(); ?>">
<?php
}
} else {
?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php
}
wp_reset_postdata();
}
?>
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post__not_in' => get_option( 'sticky_posts' ),
'paged' => $paged // ah, the page offset
);
// can be done via new WP_Query but today I am lazy
// also see http://codex.wordpress.org/Function_Reference/query_posts
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
get_template_part( 'content', get_post_format() );
}
} else {
get_template_part( 'content', 'none' );
}
wp_reset_query();
?>