admin管理员组文章数量:1130349
How can I get all subcategories and the posts related to it
$term = get_queried_object();
if($term->post_parent !=0 ){
// echo 'has parent'; //this post category has child
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
// echo $postcat ;
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
?>
// get the categories and if have subcategory get it
<?php }?>
<?php }
else{
//the posts in a subcategory
while(have_posts()):the_post();
// get the subcategory posts
<?php endwhile ?>
<?php }?>
Note: this is a code for custom taxonomy and the code here exist in a file called taxonomy-($taxonomy_name)
How can I get all subcategories and the posts related to it
$term = get_queried_object();
if($term->post_parent !=0 ){
// echo 'has parent'; //this post category has child
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
// echo $postcat ;
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
?>
// get the categories and if have subcategory get it
<?php }?>
<?php }
else{
//the posts in a subcategory
while(have_posts()):the_post();
// get the subcategory posts
<?php endwhile ?>
<?php }?>
Note: this is a code for custom taxonomy and the code here exist in a file called taxonomy-($taxonomy_name)
Share Improve this question edited Jan 2, 2019 at 23:09 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Jan 2, 2019 at 22:52 Nader ElsayedNader Elsayed 1114 bronze badges1 Answer
Reset to default 1Try using these codes:
if( isset( $sub_category ) ){
echo '<b>more items in: </b>' . $sub_category->name;
$args = array(
'cat' => $sub_category->term_id,
'post__not_in' => array( get_the_ID() )
);
$relatedpostsinsubcategory = new WP_Query( $args );
if( $relatedpostsinsubcategory->have_posts() ){
while( $relatedpostsinsubcategory->have_posts() ){
$relatedpostsinsubcategory->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php
}
wp_reset_postdata();
}
}
You can find more information about getting subcategories and related posts here.
How can I get all subcategories and the posts related to it
$term = get_queried_object();
if($term->post_parent !=0 ){
// echo 'has parent'; //this post category has child
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
// echo $postcat ;
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
?>
// get the categories and if have subcategory get it
<?php }?>
<?php }
else{
//the posts in a subcategory
while(have_posts()):the_post();
// get the subcategory posts
<?php endwhile ?>
<?php }?>
Note: this is a code for custom taxonomy and the code here exist in a file called taxonomy-($taxonomy_name)
How can I get all subcategories and the posts related to it
$term = get_queried_object();
if($term->post_parent !=0 ){
// echo 'has parent'; //this post category has child
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
// echo $postcat ;
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomy_name );
?>
// get the categories and if have subcategory get it
<?php }?>
<?php }
else{
//the posts in a subcategory
while(have_posts()):the_post();
// get the subcategory posts
<?php endwhile ?>
<?php }?>
Note: this is a code for custom taxonomy and the code here exist in a file called taxonomy-($taxonomy_name)
Share Improve this question edited Jan 2, 2019 at 23:09 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Jan 2, 2019 at 22:52 Nader ElsayedNader Elsayed 1114 bronze badges1 Answer
Reset to default 1Try using these codes:
if( isset( $sub_category ) ){
echo '<b>more items in: </b>' . $sub_category->name;
$args = array(
'cat' => $sub_category->term_id,
'post__not_in' => array( get_the_ID() )
);
$relatedpostsinsubcategory = new WP_Query( $args );
if( $relatedpostsinsubcategory->have_posts() ){
while( $relatedpostsinsubcategory->have_posts() ){
$relatedpostsinsubcategory->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php
}
wp_reset_postdata();
}
}
You can find more information about getting subcategories and related posts here.
本文标签: categoriesGet all subcategories and related posts
版权声明:本文标题:categories - Get all subcategories and related posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749048656a2308265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论