最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - In category.php I need to get the next 10previous 10 posts

programmeradmin0浏览0评论

I have a site that uses categories to output posts into categories.php. I don't have single posts using single.php

What I need is for a 'next 10' / 'previous 10' links in the category a site visitor is looking at. Here's my code from category.php

<?php if (is_category('black-metal')) : ?>
<h2><img src="<?php bloginfo('template_directory'); ?>/assets/img/black-metal-h2.png" alt="Black Metal" /></h2>
<?php $catquery = new WP_Query( 'cat=2&posts_per_page=10' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php the_content(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;
wp_reset_postdata();
?>

<?php elseif (is_category('death-metal')) : ?>
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=10' ); ?>
<h2><img src="<?php bloginfo('template_directory'); ?>/assets/img/death-metal-h2.png" alt="Death Metal" /></h2>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?> 
<?php the_content(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;
    wp_reset_postdata();
?>

So for example if someone is looking at the Black Metal category, I want next 10/previous 10 links for that particular category.

Hope that makes sense. I've done a lot searching but haven't been able to find an answer to this.

I have a site that uses categories to output posts into categories.php. I don't have single posts using single.php

What I need is for a 'next 10' / 'previous 10' links in the category a site visitor is looking at. Here's my code from category.php

<?php if (is_category('black-metal')) : ?>
<h2><img src="<?php bloginfo('template_directory'); ?>/assets/img/black-metal-h2.png" alt="Black Metal" /></h2>
<?php $catquery = new WP_Query( 'cat=2&posts_per_page=10' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php the_content(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;
wp_reset_postdata();
?>

<?php elseif (is_category('death-metal')) : ?>
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=10' ); ?>
<h2><img src="<?php bloginfo('template_directory'); ?>/assets/img/death-metal-h2.png" alt="Death Metal" /></h2>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?> 
<?php the_content(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;
    wp_reset_postdata();
?>

So for example if someone is looking at the Black Metal category, I want next 10/previous 10 links for that particular category.

Hope that makes sense. I've done a lot searching but haven't been able to find an answer to this.

Share Improve this question asked Mar 11, 2020 at 20:43 KevKev 572 silver badges12 bronze badges 2
  • is '10 posts per page' the same number as you have set under 'Settings' - 'Reading'? generally, start by reading developer.wordpress/themes/functionality/pagination – Michael Commented Mar 11, 2020 at 21:04
  • Yes it is exactly the same. – Kev Commented Mar 11, 2020 at 21:06
Add a comment  | 

2 Answers 2

Reset to default 0

if the only difference in the category archives is the images, you can work without a custom query. in this case you could use (as suggested here https://developer.wordpress/themes/functionality/pagination/#simple-pagination ):

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>     
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

or any other suggestion from https://developer.wordpress/themes/functionality/pagination/

for the image, you need to change the img line in your category.php:

<h2><img src="<?php bloginfo('template_directory'); if( is_category( 'black-metal') ) { echo '/assets/img/black-metal-h2.png" alt="Black Metal"'; } elseif( is_category( 'death-metal') ) { echo '/assets/img/death-metal-h2.png" alt="Death Metal"'; } ?>/></h2>

I found the answer was quite simple in the end. Thanks for all the input. The answer I needed wa here: https://developer.wordpress/reference/functions/the_posts_pagination/

发布评论

评论列表(0)

  1. 暂无评论