I'm displaying blog posts on a page and have enabled pagination. However, the pagination doesn't seem to quite work.
When you navigate to the second page it displays the same posts as on the first page.
This is my code:
<div class="container-max">
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1'; ?>
<?php
$args = array( 'numberposts' => 5, 'offset' => 2, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date",'cat'=>'-8, -9, -7, -6, -5, -4','paged'=> $paged);
$postslist = get_posts( $args );
echo '<div class="latest_new_posts main-news">';
?>
<?php
$wp_query = new WP_Query($args);
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : the_post(); ?>
<?php foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="position:relative; margin:45px 0;" >
<div class="blog-date-news main-news-date"><span><?php the_date('d/m/Y'); ?></span></div>
<div class="blog-container main-news-container">
<div class="news-blog-title"><span><?php the_title(); ?></span></div>
<div class="news-blog-excerpt"> <?php echo excerpt(500); ?> </div>
<div class="news-blog-more main-news-blog-more"> <img src="http://www.mariadev.co.uk/wp-content/uploads/2017/05/read-more.png"/></div>
</div>
</div>
<?php endforeach; ?>
<?php endwhile; ?>
<div class="blog-page-next"><?php next_posts_link( 'Next', $wp_query->max_num_pages );
?> </div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
Any ideas what I'm doing wrong?
Replace 'numberposts' => 5 with 'posts_per_page' => 5
Can you use query_posts() ?
$postslist = get_posts( $args );
replace
$postslist = query_posts( $args );
Related
I'm trying to add pagination to a custom loop, but I can't figure out how to do it. When I manage to add "previews" and "next" buttons it always shows the same 10 posts. I found some solutions for a while loop but not for a foreach loop (which I actually never used before).
This is the loop (is get_posts a problem?) :
<?php
$news = get_posts(array('posts_per_page' => 10));
$news['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?>
<?php foreach ($news as $article): ?>
<div class="col-md-4">
<h3><?php echo $article->post_title ?></h3>
<hr>
<p class="desc"><?php echo $article->post_excerpt ?></p>
<?php echo get_the_post_thumbnail($article->ID,'thumbnail'); ?>
<p class="btn_text"> Ler mais</p>
</div>
<?php endforeach; ?>
<?php previous_posts_link( '<<' );
next_posts_link( '>>', $custom_query->max_num_pages ); ?>
<?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;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source : http://web-profile.net/wordpress/themes/wordpress-custom-loop/
Each page returns the same posts when you click "older posts". This was working fine at one time, but now it's not. Here is the code that I'm using:
<?php if ( have_posts() ) : ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 6, 'paged' => $paged, 'post_status' => 'publish', 'offset'=> 0, 'category'=> '-1, -9, -16, -17, -119, -318' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="blog-post-wrap">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="post-thumb blog-index">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'large' );
}
?>
</div>
<div class="title-meta-wrap">
<?php if (strlen($post->post_title) > 75) {
echo substr(the_title($before = '', $after = '', FALSE), 0, 75) . '...'; } else {
the_title();
} ?>
</a>
<p><span class="theauthor"><?php the_date(); ?> <span style="color:#EC7906;"> / </span><?php the_author(); ?> </span></p>
</div>
</div>
<?php endforeach;
wp_reset_postdata();?>
<?php endif; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
I was able to remove this:
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
And go with just this:
get_posts( $args );
And pagination worked but the page only returned one post per page. Any thoughts?
I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category.
Can someone please help em to add a category there one is "surf" , I've tried everything.
Or maybe a different way to do it?
Thank you.
<div class="posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title section small-padding">
<h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div> <!-- /post -->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="archive-nav">
<?php echo get_next_posts_link( '« ' . __('Posts Antigos', 'radcliffe')); ?>
<?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' »'); ?>
<div class="clear"></div>
</div> <!-- /post-nav archive-nav -->
<?php endif; ?>
<?php endif; ?>
</div> </div> <!-- /posts -->
If you are trying to display category by ID , Then
global $post;
$args = array( 'category' => '12' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Note: In $args = array( 'category' => '12' ); 12 is the ID of
the category
But if you want to display category by Name, Then
global $post;
$args = array( 'category_name' => 'uncatogerized' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Here, uncategorized is a category name
I have problem to change content on page when for ex. url change from
/?page_id=55&paged=1 to /?page_id=55&paged=2...
So I have problem to display next/previous page content, it just show first 4 post on page and when I click next nothing hapend with content just url change.
Here is my blog template:
<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
<div class="row main-part">
<?php
$args = array(
'category_name' => 'blog',
'paged' => $paged,
'posts_per_page' => 4
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
?>
<div class="col-xs-12 col-md-3">
<h1><?php the_title(); ?></h1>
<p>
<?php the_excerpt(); ?>
</p>
<p><a class="btn btn-primary" href="<?php the_permalink() ?>" role="button">View details »</a></p>
</div>
<?php endwhile;endif; ?>
<?php
wp_reset_postdata();
wp_reset_query();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
<?php get_footer(); ?>
I'm trying to use WP pagination on post archive page but exclude posts from one category to be shown there.
When I add this to my code the page2,3,4... of the archive display the same first 10 posts:
<?php query_posts('cat=-4');?>
This is the whole code of my page template so I would be grateful for all your help:
<?php
/*
Template Name: Post archive
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="content col-md-9">
<div class="home-content">
<!-- Show posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php query_posts('cat=-4');?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<div style="float:left; margin:1%;">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'thumbnail', array( 'class' => 'img-post')); // show featured image
}
?>
</div>
<h1 class="post-thumb"><?php the_title(); ?></h1>
<h4>Category: <?php the_category(', '); ?></h4>
<p><?php the_excerpt(); ?></p>
<hr style="margin-bottom:5%">
<?php endwhile; ?>
<!-- pagination -->
<div class="nav-previous alignleft" style="margin-top:-1%"><?php next_posts_link( 'See older posts' ); ?></div>
<div class="nav-next alignright" style="margin-top:-1%"><?php previous_posts_link( 'See newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-3 sidebar unstyled">
<?php dynamic_sidebar( 'home1' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'home2' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'articles1' ); ?>
</div>
</div>
</div>
</body>
</html>
<?php get_footer(); ?>
This code fixed my page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat'=> -4,
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
Modifying to reflect OP's solution for benefit of future readers
Change query() slightly as shown below
$args = array(
'cat'=> -4,
'posts_per_page'=> 10,
'paged'=> $paged
);