[英]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.