2

I have a custom post type and a taxonomy with children, with a template file named taxonomy-productcategories.php. On this i want to list all children with their posts.

  • Child taxonomy 1

    • Child taxonomy 1 Post A
    • Child taxonomy 1 Post B
    • Child taxonomy 1 Post C
  • Child taxonomy 2

    • Child taxonomy 2 Post D
    • Child taxonomy 2 Post E
    • Child taxonomy 2 Post F

And so on...

I have an example for getting just the Child taxonomy, but i want to add the post from each taxonomy.

<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );

foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName );
    echo "$term->name:<br />"; 

}
?>
2
  • The solution I would go with is probably using WP_Query() but that might be inefficient / slow but it would get the job done.
    – Howdy_McGee
    Nov 12, 2013 at 20:59
  • Yes, that was my thought too, som i looked fore that type of solution, but the only thing i found was geting it done with a specific post type and taxomony.
    – Gustav
    Nov 13, 2013 at 7:55

1 Answer 1

0

I had an idea of combining the first code with this solution that lists posts from a specifik taxomony:

<?php $terms = get_terms('productcategories');
    foreach ($terms as $term) {
        $wpq = array (
        'taxonomy'=>'productcategories',
        'term'=>$term->slug,
        'order'=>'asc',
        'orderby'=>'title');
        $query = new WP_Query ($wpq);
        echo "$term->name:<br />"; 
        ?>
        <?php
        if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
        <a href="<?php the_permalink();?>"><?php the_title();?></a>, 
        <?php endwhile; endif; wp_reset_query(); ?>
        <?php
        echo "<br />";   
    }
?>

I managed to combine the two, so this is my current solution. Maybe not the best but it works.

<?php 
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
    $termchildren = get_term_children( $current_term->term_id, $taxonomyName );
    foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName );
        $wpq = array (
        'taxonomy'=>$taxonomyName,
        'term'=>$term->slug,
        'order'=>'asc',
        'orderby'=>'title');
        $query = new WP_Query ($wpq);
        echo "$term->name:<br />"; 
        ?>

        <?php
        if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
        <a href="<?php the_permalink();?>"><?php the_title();?></a>, 
        <?php endwhile; endif; wp_reset_query(); ?>
        <?php
        echo "<br />";   
    }
?>
2
  • 2
    Instead of adding 2 answers, you should edit your first answer and append this code to it explaining the difference.
    – Howdy_McGee
    Nov 13, 2013 at 14:45
  • Done. Still working on a way to order the children. Right now i think it ordered bi ID.
    – Gustav
    Nov 15, 2013 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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