在wordpress主页上只显示一个类别


Showing only one category on wordpress homepage

我想在我的主页上只显示一个类别。要做到这一点,我知道我必须编辑index.php文件,但我不知道为什么我的代码不采取。在本例中,我要显示的文章类别的ID为"1"

下面是代码(我编辑的是最后一行):

<div class="row">
    <!-- Contains the loop of all posts -->
    <div class="col-md-8" id="post-container">
        <!-- Start the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <?php if (in_category('1')&& is_home()) continue; ?>

任何想法?

免责声明:我在没有办法流利的php,我只是被告知,这应该是一个容易解决的问题。

为了正确地做到这一点,你需要在你的主页数据上运行一个过滤器…其他答案和您正在做的事情将无法与分页正常工作…例如,如果最近的X篇文章不在你想要显示的类别中,你将得到0个结果或更少的结果。

你想以这样的方式运行过滤器…

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', 'YOUR CATEGORY ID HERE' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

这将正常工作,并让您仍然使用适当的分页。你可以在这里找到更多关于过滤器的信息-> http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

巴特

尝试<?php if (in_category(1)&& is_home()) continue; ?>。ID必须是一个整数,否则会被认为是一个分段。

更多参数信息

<?php $args = array(
    'orderby'            => 'name',
    'order'              => 'ASC',
    'style'              => 'list',
    'include'            => '',
    'hierarchical'       => 1,
    'title_li'           => __( 'Categories' ),
    'show_option_none'   => __( 'No categories' ),
    'number'             => 1,
    'taxonomy'           => 'category',
    'walker'             => null
); ?>
<?php if (is_home()) { ?>
 <ul class="categories">
   <?php wp_list_categories($args); ?>
 </ul>
<?php } ?>

与其浏览所有帖子并筛选出你想要的帖子,

尝试使用

(

查询_文章"猫= 1");

//这将改变主循环,然后你可以有更多的控制,如每页post等等

如果(have_posts()):虽然(have_posts ()): the_post ();