简体   繁体   English

如何显示特殊父母类别孩子的相关wordpress帖子?

[英]How can I show related wordpress post from special parent category child?

I work in my Single.php template for Wordpress theme, but in related posts I have problem! 我在Wordpress主题的Single.php模板中工作,但在相关文章中却遇到了问题!

I want show related posts from parent category child for example I add parent category with A name in Wordpress, in this parent category we have B,C,D and other child categories that can set for every post in Wordpress posts area. 我想显示父类别子项的相关帖子,例如,我在Wordpress中添加具有A名称的父类别,在此父类别中,我们可以为Wordpress帖子区域中的每个帖子设置B,C,D和其他子类别。

Well I publish new post in D (or other A children) category I want show in related posts box, other from D (or other A children) category. 好吧,我要在D(或其他A子级)类别的其他帖子中显示新的D(或其他A子级)类别的帖子。

this is my work but not good 这是我的工作,但是不好

$related = get_posts( array(
'category__in' => wp_get_post_categories( $post->ID ),
'numberposts'  => 3,
'post__not_in' => array( $post->ID )
) );

I'm newbie please help me, thanks. 我是新手,请帮助我,谢谢。

This code wil get a taximony with a parent so in your case it wil get the child but if you can have a post that is A->B AND A->C it wil only get related posts from the first catagory selected eg(A->B) 该代码将与父母打成一片,因此在您的情况下它将生下孩子,但如果您可以发布A-> B AND A-> C的帖子,则只会从所选的第一个类别中获得相关的帖子,例如(A -> B)

$terms = wp_get_post_terms($post->ID, 'category');
if (count($terms)) {
    foreach ($terms as $term) {
        if ($term->parent != 0) {
            $relatedTerm = $term;
            break;
        }
    }
    $related = get_posts(array(
        'category__in' => $relatedTerm->term_id,
        'numberposts'  => 3,
        'post__not_in' => array($post->ID),
    ));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Wordpress中从其父类别中排除子帖子? - How can I exclude child post from its parent category in Wordpress? 如何从父类别中排除特定的子类别帖子? - How can I exclude a specific child category post from a parent category? WordPress:如何使子类别中的博客显示在父类别中 - Wordpress: How To Make Blogs In Child Category Show Up In Parent Category 如何在Wordpress中从具有多个类别的帖子中返回子类别? - How can I return a child category from a post with multiple categories in Wordpress? 同一子类别的相关文章-Wordpress - Related Articles from same child category - wordpress 如何按类别显示不同帖子类型的相关帖子 - How to show related posts by category from different post type 如何在WordPress中按类别显示相关产品 - How to Show Related Products by Category in WordPress 如何在Wordpress帖子和页面中显示类别中的项目 - How to show Items in a category from Wordpress post and page 当有人点击WordPress主题中的帖子时,如何在侧栏显示相关的类别帖子? - How to show related category posts in side bar when someone click on the post in WordPress theme? 如何在wordPress中按分类法类别ID或分类法类别名称获取所有分类法类别的帖子? - How can i get all post of taxonomy category post by taxonomy category id or taxonomy category name in wordPress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM