wordpress - category.php 中的自定义帖子数量

标签 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 if ( have_posts() ) : while ( have_posts() ) : 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();

最佳答案

一切都已经在 24 中设置好了,因此您无需使用自定义查询修改任何内容。

这是你应该做的:

  • 创建一个 child theme这样您就可以进行修改,以免丢失任何更新工作
  • 使用 pre_get_posts调整类别页面上的主要查询以显示每页 15 个帖子。

  • 将以下内容添加到您的子主题 functions.php 或自定义插件中(请注意,由于使用了闭包,您需要安装 PHP 5.3+ 才能正常工作)
    add_action( 'pre_get_posts', function ( $query )
    {
        if ( !is_admin() && $query->is_main_query() && $query->is_category() ) {
            $query->set( 'posts_per_page', 15 );
        }
    });
    

    关于wordpress - category.php 中的自定义帖子数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30662593/

    相关文章:

    php - 如何使自定义帖子元 wp_editor 可翻译?

    wordpress - 当自定义帖子类型设置了 show_in_menu 时缺少分类菜单

    php - 在 WooCommerce 购物车页面中的每个产品下方添加运输类别

    html - 为什么幻灯片没有出现在我的自定义主题中?

    php - 在 WAMP 上安装 wordpress 仅提供空表

    c++11 - 什么样的迭代器支持随机访问但不支持 C++ 中的连续存储?

    wordpress - 新模板未显示在"template"下拉列表中

    css - 如何根据WP上帖子的类别加载不同的CSS

    wordpress - 如何在 WordPress 中获取帖子中的类别标题?

    wordpress - 在 WordPress 中创建隐藏的自定义帖子类型