Wordpress category.php中的自定义帖子数量
我想在我的category.php中设置不同数量的帖子。我想显示15篇文章与分页每页 我用的是二十四主题。我怎样才能做到这一点 我的代码是:Wordpress category.php中的自定义帖子数量,wordpress,categories,custom-post-type,Wordpress,Categories,Custom Post Type,我想在我的category.php中设置不同数量的帖子。我想显示15篇文章与分页每页 我用的是二十四主题。我怎样才能做到这一点 我的代码是: 试试这个代码 <?php get_header(); ?> <section id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php $que
试试这个代码
<?php
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$query = new WP_Query(array(
'posts_per_page' => 15
));
if ( have_posts() ) : while ($query->have_posts()): $query->the_post();
?>
<div class="post-cat">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_category( ', ' ); ?></p>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div><!-- #content -->
</section><!-- #primary -->
<?php
get_sidebar( 'content' );
get_sidebar();
get_footer();
?>
在2144年,一切都已经很好地设置好了,因此您不需要使用自定义查询修改任何内容
以下是您应该做的:
- 创建一个更新,这样您就可以进行修改,这样您就不会丢失任何关于更新的工作
- 使用
pre_get_posts
调整类别页面上的主查询,使其每页显示15篇文章
将以下内容添加到您的子主题functions.php或自定义插件(请注意,由于使用闭包,您需要安装PHP5.3+才能使其正常工作)
回答得很好,+1用于使用pre\u get\u posts
感谢您的帮助。竖起大拇指!:)正如@user4717687所说,您的解决方案将不起作用,您的代码将返回最新的15篇文章,而不考虑类别,因此您实际上已经破坏了类别页面功能
add_action( 'pre_get_posts', function ( $query )
{
if ( !is_admin() && $query->is_main_query() && $query->is_category() ) {
$query->set( 'posts_per_page', 15 );
}
});