如何在wordpress中仅获取父类别帖子

编程入门 行业动态 更新时间:2024-10-21 23:21:58
本文介绍了如何在wordpress中仅获取父类别帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想获得首页中唯一的顶级父类别(cat_id=1)列表.为此,我在主页中编写了这个 While 循环.

<?php而(有帖子()){the_post();?><div class="content-block col-xs-12"><h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3><hr class="color-strip"><p><?php echo wp_trim_words(get_the_content(), 11);?></p><p><a href="<?php the_permalink(); ?>">继续阅读&raquo;</a></p><div class="tags"><ul><li><?php the_tags();?></li></ul></div>

<小时/><?php }?>

但它也带来了所有的子类别.我只想保留一个类别的列表.我必须在 if 条件中进行哪些更改.

解决方案

您可以使用 get_categories() 并且您可以获得所有顶级类别:

$categories = get_categories(大批('父母' =>0));

然后,像这样的事情应该适合您只获取顶级类别中的帖子.

$categories = get_categories(大批('父母' =>0));$categoryArray = array();foreach( $categories 作为 $category ) {$categoryArray[] = $category->ID;}函数 exclude_category($query) {如果 ( $query->is_home() ) {$query->set('cat', implode(',', $categoryArray));}返回 $query;}add_filter('pre_get_posts', 'exclude_category');

这将获取所有顶级类别,然后在主查询中设置它们.

I want to get the only top parent categories(cat_id=1) list in the front page. For that I have written this While loop in home page.

<div class="left col-xs-12 col-md-8 col-lg-9">
        <?php
            while(have_posts()) {
                the_post(); ?>
                <div class="content-block col-xs-12">
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <hr class="color-strip">
                    <p><?php echo wp_trim_words(get_the_content(), 11); ?></p>
                    <p><a href="<?php the_permalink(); ?>">Continue reading &raquo;</a></p>
                    <div class="tags"><ul><li><?php the_tags(); ?></li></ul></div>
                </div>
                <hr/>
        <?php }?>

    </div>

But its bringing all the subcategories also. I want to keep the list of only one category. What change do I have to make in the if condition.

解决方案

You can get the categories using get_categories() and you can get all of the top level categories with:

$categories = get_categories(
    array(
        'parent' => 0
    )
);

Then, something like this should work for you to get only posts within top level categories.

$categories = get_categories(
    array(
        'parent' => 0
    )
);

$categoryArray = array();

foreach( $categories as $category ) {
    $categoryArray[] = $category->ID;
}

function exclude_category($query) {
    if ( $query->is_home() ) {
        $query->set( 'cat', implode(',', $categoryArray) );
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_category');

This will get all of the top level categories and then set them in the main query.

这篇关于如何在wordpress中仅获取父类别帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 03:24:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类别   帖子   如何在   wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!