I think it has something to do with the main loop. I don't want to include a category in the main loop so I have done the following.
However, it just shows the most recent posts on each page when I go to the next one in the pagination. What am I doing wrong?
<?php query_posts('cat=-219'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<div style="clear:both"></div>
<nav>
<div class="pager">
<div class="pager-btn" id="next-pager"><?php next_posts_link( 'Next' ); ?></div>
<div class="pager-btn" id="last-pager"><?php previous_posts_link( 'Last' ); ?></div>
</div>
</nav>
<?php endif; wp_reset_query(); ?>
I've tried changing it to only include the categories I want instead of excluding the one but it still does the same. It works fine if I just include one category though (eg I put cat=1
).
I can recommend you to make queries with proper arguments, like this:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3 (or -1 for all post),
'cat'=>219
'post_status'=>'publish',
'order' => 'DESC',
'paged' => $paged
);
$the_query = new WP_Query($args);
Then in your loop, you could do this:
if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<div style="clear:both"></div>
<nav>
<div class="pager">
<?php if (function_exists("pagination")){pagination($custom_query->max_num_pages);} ?>
</div>
</nav>
<?php endif; wp_reset_query();?>
Then copy the Pagination Code in functions.php:
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
More information you can find at https://developer.wordpress.org/reference/functions/query_posts/
Solved by adding this to functions.php.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-219' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.