John Davidson

php - List posts grouped by taxonomy and child taxonomies

0 comments
Message:


I have the following post structure on my dash


<ul>
<li>Tax 1
<ul>
<li>Tax 2<br>
<span>- Post 1</span><br>
<span>- Post 2</span>
</li>
<li>Tax 2
<ul>
<li>Tax 3<br>
<span>- Post 1</span><br>
<span>- Post 2</span>
</li>
</ul>
</li>
</ul>
</li>
<li>Tax 1
<ul>
<li>Tax 2<br>
<span>- Post 1</span><br>
<span>- Post 2</span>
</li>
<li>Tax 2
<ul>
<li>Tax 3<br>
<span>- Post 1</span><br>
<span>- Post 2</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>

I need to show each post as a child of the last category related to it, I created a scheme but it does not group the sub categories


    <?php 
$member_group_terms = get_terms( 'categoria' );
?>

<?php
foreach ( $member_group_terms as $member_group_term ) {
$member_group_query = new WP_Query( array(
'post_type' => 'documento',
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'field' => 'slug',
'terms' => array( $member_group_term->slug ),
'operator' => 'IN'
)
)
) );
?>
<h2><?php echo $member_group_term->name; ?></h2>
<ul>
<?php
if ( $member_group_query->have_posts() ) : while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
<li><?php echo the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>
<?php
// Reset things, for good measure
$member_group_query = null;
wp_reset_postdata();
}
?>

The problem here is that it does not group the subcategories, leaving them listed as if it were the level 1 category.I need it to display as I mentioned above, grouped by sub categories.



My Answer:
board:

Post 1:
Title: "Best Restaurants in New York City"
Taxonomy: Food
Child Taxonomy: Restaurants

Post 2:
Title: "Top Hiking Trails in California"
Taxonomy: Outdoor Activities
Child Taxonomy: Hiking

Post 3:
Title: "Must-Visit Museums in Europe"
Taxonomy: Travel
Child Taxonomy: Museums

I want to display a list of posts grouped by their taxonomy and child taxonomies. How can I achieve this in PHP?

Rate this post

5 of 5 based on 4291 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us