0

I am trying to pull a list of custom taxonomies from a CT of business-types. I am looping round them like so:

<?php $terms = get_terms("business-types");
    foreach ( $terms as $term ) { ?>
        <div>
            <img src="<?php the_field('category-image', $term); ?>" alt="" />
            <a href="<?php echo esc_url( get_term_link( $term ) ) ?>"><?php echo $term->name; ?></a>
        </div>
<?php } ?>

This works and is listing the name with a link, and an image as assigned in the backend with ACF. Now, one of these taxonomies has children, so the idea is that if you click one with children, you're then shown the sub-children of that business-type. This doesn't work, it just lists all of them. Here's an example of the CT layout:

  • Professional
  • Retail
  • Hospitality
    • Restaurants
    • Coffee Shops
    • Takeaway

So basically, you click businesses and it shows you professional, retail and hospitality. You click hospitality and the same template is used to show restaurants, coffee shops and takeaway. Then you click one of those and it lists all posts (In a CPT) with that term.

Currently, you click hospitality and it just lists everything under that... Any ideas on how I can do this multi-level in the same template?

edit: it seems to be outputting all terms which have posts assigned to them... Even the children items. I also need to know how to show only top level items for the term you're in...

Cheers, Ash

  • Any code to show us? – Max Yudin Aug 18 '16 at 14:08
  • It's at the top of the question? :) The rest is just containers, header and footer... Unless you mean for the custom taxonomy? – AshboDev Aug 18 '16 at 14:11
  • No-no! What where the steps to achieve this? I have a code covering your problem, but I want you to be in charge, not me. What you already had found? – Max Yudin Aug 19 '16 at 17:08
  • It's not the place where you can get the code for free, here you can get a direction where to move to. I'll wait a month and then I'll paste my code here for future users. I have a piece of code covering your problem. – Max Yudin Aug 19 '16 at 17:15
0

This is a direction. Not tested but you have an idea.

<?php
// get current term
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
// get parent term
$parent = get_term($term->parent, get_query_var('taxonomy') );
// get children
$children = get_term_children($term->term_id, get_query_var('taxonomy'));

$taxonomy = 'business-types';

if ( empty($parent->term_id) ) {
    echo '<a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a><br />';
    $args = array(
        'child_of' => $term->term_id,
        'taxonomy' => $term->taxonomy,
        'hide_empty' => 1,
        'hierarchical' => true,
        'depth'  => 1
    );

    echo '<ul>';
    wp_list_categories($args);
    echo '</ul>';

// Subcategories
} elseif ($parent->term_id && sizeof($children) == 0) {
    $subterm = get_queried_object();
    // $parents = $subterm->parent; // May be will be useful in the future
    // $term_id = $subterm->term_id; //May be will be useful in the future
    echo '<a href="' . esc_url( get_term_link( $term ) ) . '">' . $subterm->name . '</a><br />';
    $args = array (
        'taxonomy'  => $taxonomy,
        'pad_counts'=> 0,
        'title_li'  => '',
        'child_of'  => $subterm->parent,
    );

    echo '<ul>';
    wp_list_categories($args);
    echo '</ul>';
}

For Parent Term listing you have to go here.

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.