I have a static front page on my wordpress site and am trying to get pagination working.
The pagination actually works if I manually go to the address (/2/, /3/ etc) but the next_posts_link and previous_posts_links don't work at all.
Could anyone pinpoint what my error is here?
<?php $args = array(
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'category__in'=> $cat,
'paged' => $paged,
);
$featured_query = new WP_Query($args);
while ($featured_query->have_posts()) : $featured_query->the_post();
?>
<?php get_template_part( 'template-parts/content', get_post_format() ); ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts', $featured_query->max_num_pages ); ?></div>
<?php if(is_paged()) { ?>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts', $featured_query->max_num_pages ); } ?></div>
You should check your scripts , it maybe and actual script file that you can't reach , inspect that certain element.
Please try below code
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_blog = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => get_option('posts_per_page ')
);
$query_blog = new WP_Query( $args_blog );
if($query_blog->have_posts()) : while($query_blog->have_posts()) : $query_blog->the_post();
?>
<div class="pagination">
<?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') ),
'prev_text' => __('Prev'),
'next_text' => __('Next'),
'total' => $query_blog->max_num_pages
) );
?>
</div>
<?php
else :
get_template_part( 'content', 'none' );
endif;
?>
Try this code:
You should call <?php wp_reset_postdata(); ?> after post link see updated code:
<?php
$args = array(
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'category__in'=> $cat,
'paged' => $paged,
);
$featured_query = new WP_Query($args);
while ($featured_query->have_posts()) : $featured_query->the_post();
?>
<?php get_template_part( 'template-parts/content', get_post_format() ); ?>
<?php endwhile;?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts', $featured_query->max_num_pages ); ?></div>
<?php if(is_paged()) { ?>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts', $featured_query->max_num_pages ); } ?></div>
<?php wp_reset_postdata(); ?>
Related
I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
The "Older" and "Newer" links show correctly, and it updates the url to reflect the page number but the posts content remains the same as it does on the first page when flipping through the pages. Here is the code I'm using, simplified of course:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
$get_featured_posts = new WP_Query( array(
'posts_per_page' => 9,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'paged' => $paged,
'offset' => 5
) );
while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
<h3 class="entry-title entry-added">
<?php the_title(); ?>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more">Read More</p>
<?php
endwhile;
?>
<?php
// Reset Post Data
wp_reset_query();
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Can't seem to figure out what the problem is, any help would be appreciated.
try to use wp_reset_postdata(); instead of wp_reset_query(); and also updated condition for $paged.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$get_featured_posts = new WP_Query( array(
'posts_per_page' => 9,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'paged' => $paged,
'offset' => 5
) );
while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
<h3 class="entry-title entry-added">
<?php the_title(); ?>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more">Read More</p>
<?php
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
?>
Please read this and insert that code of pagination on your page and remove newer and older links
https://www.elegantthemes.com/blog/tips-tricks/how-to-add-pagination-to-wordpress
Figured out the problem thanks to #milo on the WordPress exchange. Turns out there are special considerations when using "offset" and pagination. Check this link for more details.
How i can enable pagination in my page (template page wordpress)
My Code
<?php
$catquery = new WP_Query( 'cat=2&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<br />
<div class="news"><!-- Start News Box -->
<div class="img_news"><!-- Start Image News -->
<?php
$url_thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img class="img_thumbs" title="" alt="" src="<?php echo $url_thumb; ?>">
</div><!-- End Image News -->
<div class="title_news"><!-- Start Title News -->
<h2>
<?php the_title(); ?>
</h2>
<div class="details">
<?php the_content_limit(500, "Read More..."); ?>
</div>
</div><!-- End Title News -->
<hr>
</div><!-- End News Box -->
</div>
<?php endwhile; ?>
I am using this but i can't see the pagination bar : example (1-2-3-...-100)
Thanks
you need to add 'paged' attribute in argument($catquery).
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$catargs = array('cat'=>'2','posts_per_page'=>10,'paged' => $paged);
$catquery = new WP_Query( $catargs);
while($catquery->have_posts()) : $catquery->the_post();
//do stuff
endwhile;
$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' => $catquery->max_num_pages,
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
) );
You should add these pagination functions before or after your loop (before or after the while loop):
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Have a look at Wordpress' documentation here.
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>