Tree of category and subcategory and the posts titles (custom post type)
-
At the begining I would like to greet all the WordPress community and excuse all for my English!
I made a page using the Custom Post Type. I created categories and sub-categories, and I added to them posts. Now, on one site I want to show it all. The following code throws me everything but not exactly like I wanted. So there is a problem with the overarching categories because the script throws all posts belonging to the sub-category also included in each category parent. If this were the usual entries I could used in_category function. But these are Custom Post Type and this feature does not work. How to replace in_category function in the Custom post types?
Below my code:
<?php $categories = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => 0 , 'hide_empty'=> '0' )); foreach ( $categories as $category ) { if ( $category->parent > 0 ) { continue; } $i = 0; echo '<h1 style="font-weight:bold">' . $category->name . '</h1>'; $posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'MY_CUSTOM_POST_TYPE' ) ); echo '<ul>'; foreach ( $posts as $post ) { $child_categories = get_term_children( $category->term_id, 'MY_CUSTOM_TAXONOMY' ); if ( $child_categories && in_category( $child_categories, $post->ID ) ) { continue; } setup_postdata($post); echo '<li>'; the_title(); echo '</li>'; } echo '</ul>'; $categories2 = get_terms('MY_CUSTOM_TAXONOMY',array('parent' => $category->term_id , 'hide_empty'=> '0' )); foreach ( $categories2 as $category ) { $j = 0; echo '<h2>' . $category->name . '</h2>'; $posts = get_posts( array( 'MY_CUSTOM_TAXONOMY' => $category->name, 'post_type' => 'MY_CUSTOM_POST_TYPE' ) ); echo '<ul>'; foreach($posts as $post) : setup_postdata($post); echo '<li>'; the_title(); echo '</li>'; endforeach; echo '</ul>'; } } ?>
- The topic ‘Tree of category and subcategory and the posts titles (custom post type)’ is closed to new replies.