0

Firstly, I'm trying to display all tags from a taxonomy called 'group'. However, this taxonomy currently contains two tags, from which one of of them has multiple tagchildren.

I'd lik to display all posts belonging to those children. So the final result should look something like this:

  • Parent Tag A
    • Child tag
      • Post data
    • Child tag
      • Post data
    • etc...
  • "Parent" Tag B

    <?php
    $taxonomyName = "group";
    $terms = get_terms($taxonomyName,array('parent' => 0));
    foreach($terms as $term) {
            echo '<a href="'.get_term_link($term->slug,$taxonomyName).'">'.$term->name.'</a>';
            $term_children = get_term_children($term->term_id,$taxonomyName);
            echo '<ul>';
            foreach($term_children as $term_child_id) {
                    $term_child = get_term_by('id',$term_child_id,$taxonomyName);
                    echo '<li><a href="' . get_term_link( $term_child->name, $taxonomyName ) . '">' . $term_child->name . '</a></li>';
            }
            echo '</ul>';
    }
    ?>
    
1

If anyone's interested, below is the solution that worked out for me. Credits go to Akshay Paghdar.

$taxonomyName = "group";
$terms = get_terms($taxonomyName,array('parent' => 0));
echo '<ul>';
foreach($terms as $term)
{
    echo '<li><a href="'.get_term_link($term->slug,$taxonomyName).'">'.$term->name.'</a>';
    $term_children = get_term_children($term->term_id,$taxonomyName);
    echo '<ul>';
    foreach($term_children as $term_child_id)
    {
        $term_child = get_term_by('id',$term_child_id,$taxonomyName);
        echo '<li><a href="' . get_term_link( $term_child->name, $taxonomyName ) . '">' . $term_child->name . '</a>';
        echo '<ul>';
        $tax_arg = array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomyName,
                    'field' => 'id',
                    'terms' => $term_child_id
                )
            )
        );
        $posts = get_posts($tax_arg);
        foreach($posts as $post)
        {
            echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>';
        }
        echo '</ul>';
        echo '</li>';
    }
    echo '</ul>';
    echo '</li>';
}
echo '</ul>';
  • Ah, I didn't get that you were searching for all posts as well. – kaiser Feb 6 '14 at 12:21
0

What you're searching for (from reading that you are wrapping terms in links) possibly is

echo get_the_term_list( $postID, $taxonomy, $before, $separator, $after );

To filter the list, you can use a callback on the get_the_terms filter. Internally get_the_term_list() has two filters. The first one is from it using get_the_terms() to retrieve the terms, which offers mentioned filter. The second one is term_links-{$taxonomy_name} and has a single argument, which is an array of HTML anchors pointing to the term archive pages.

You'll want to use the first one:

add_action( 'get_the_terms', 'WPSE133490getTermsCb', 10, 3 );
echo get_the_term_list(
    get_the_ID(),
    'custom_taxonomy_name',
    "<ul><li>",
    "</li><li>",
    "</li></ul>"
);

The callback would be something for your themes function.php file and goes like this - if you want to use it as plugin, else remove the first three lines.

<?php
defined( 'ABSPATH' ) or exit;
/** Plugin Name: (#133490) Remove Term Parents from link list */

function WPSE133490getTermsCb( $terms, $postID, $tax )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $filtered = wp_list_filter(
        $terms,
        array(
            'parent'          => 0,
            // Only when filtering the built in "Category"-taxonomy
            // 'category_parent' => 0,
        ),
        // AND: All args must match
        // OR:  Only one arg must match
        // NOT: Not arg must match
        'AND'
    );

    return $terms;
}
  • Thanks, but I already got the right answer from someone else. I'll post it below. – digifrog Feb 6 '14 at 10:21

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.