Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 />"; 

}
?>
share|improve this question
    
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 '13 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 '13 at 7:55
add comment

1 Answer

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 />";   
    }
?>
share|improve this answer
1  
Instead of adding 2 answers, you should edit your first answer and append this code to it explaining the difference. –  Howdy_McGee Nov 13 '13 at 14:45
    
Done. Still working on a way to order the children. Right now i think it ordered bi ID. –  Gustav Nov 15 '13 at 11:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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