0

I hope you can help me, guys. I've create custom post type and custom taxonomy with Custom Post Type UI plugin. Then i create an page taxonomy-mycustomtaxonomy.php.

When i access, for example, a url http://mysiteurl/mycustomtaxonomy/maintaxonomy1/ i have to show:

maintaxonomy1-title
maintaxonomy1-description

--

childtaxonomy1-title
childtaxonomy1-description
post1
post2
post3
...

--

childtaxonomy2-title
childtaxonomy2-description
post1
post2
...

--

...

I don't want to show all posts of maintaxonomy1. Only the child posts of that.

I start with this:

<?php $terms = get_terms("mycustomtaxonomy");
$count = count($terms);
if ( $count > 0 ){
    foreach ( $terms as $term ) {
        echo '<h2>' . $term->name . '</h2>';
        $loop = new WP_Query( array( 
            'post_type' => 'mycustomposttype',
            'post_per_page' => 100,
            'orderby' => 'date',
            'order' => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'mycustomtaxonomy',
                    'field' => 'id',
                    'terms' => $term->term_id
                )
            )
        ));
        // the loop
        while ($loop->have_posts()) : $loop->the_post();
            // do loop content
            echo '<div>' . get_the_title() . '</div>';
        endwhile;
        // reset $post so that the rest of the template is in the original context
        wp_reset_postdata();
    }
}?>

But it show posts of maintaxonomy1. Besides, show posts too of all taxonomies and i have to show only the posts of taxonomy that is in url address.

I hope you understand my question and would really apreaciate your help.

1

I got it! Seems a little confused at first, but the logic is here:

<?php $term = get_queried_object(); ?>

<?php echo $term->name; ?>

<?php 
$children = get_terms( $term->taxonomy, array(
'parent'    => $term->term_id,
'hide_empty' => false
) );
if($children) { 

foreach ( $children as $child ) {
    ?>

    <?php echo '<li>' . $child->name . '</li>'; ?>

    <?php 
        $loop = new WP_Query( array( 
            'orderby' => 'date',
            'order' => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'mycustomtaxonomy',
                    'field' => 'id',
                    'terms' => $child->term_taxonomy_id
                )
            )
        ));
        while ($loop->have_posts()) : $loop->the_post();
        ?>

        <?php echo '<div>' . get_the_title() . '</div>'; ?>

        <?php 
        endwhile;
        wp_reset_postdata();
?>

<?php }
} ?>

Hope it help others!

  • Please accept your own answer ;-) – Pieter Goosen Sep 15 '15 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.