2

I have a custom post-type "downloads", and within the downloads menu, I have download categories, the taxonomy name is "downloadcats".

My Downloads page uses a page template that lists all categories in the "downloadcats" taxonomy, this only shows parent categories. When a category is clicked, it uses the taxonomy archive template (taxonomy-downloadcats.php) and what I am trying to do is use conditional to say if there are child categories, display a list, otherwise show a list of posts.

In my taxonomy-downloadcats.php at the moment I have the following:

<?php 
$term = get_queried_object();

$children = get_terms( $term->taxonomy, array(
'parent'    => $term->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) { // get_terms will return false if tax does not exist or term wasn't found.

echo 'show child categories';
} else {
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <li><?php the_post_thumbnail('post-image'); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php endif; ?>
<?php } ?>

The conditional statement is correct as if the category has no children it does show posts and if there are child categories, it displays "show child categories", I need to know what goes in there to make the parent's children appear.

Thanks

3
  • I really do not understand what you are trying to do. What template is this. taxonomy.php will display the current term plus all of it's child terms if there are any. Are you mixing built-in categories with custom taxonomy terms. Your termonology is really confusing, and what is more confusing even is that you are talking about a custom taxonomy but making use of get_categories. Please see this post and then file an edit with all the necessary info so that I can try and help you. This can be done with a filter only. Commented Nov 21, 2014 at 5:17
  • Thanks Pieter, have edited above, sorry for the terminology, taxonomy and post-types are all new areas for me.
    – Adrian
    Commented Nov 21, 2014 at 9:14
  • Ah! That makes much more sense :-). You don't need to apologize, I just pointed out the importance of beign clear. Remember, nobody needs to help anybody, so if you really need someone to help you, you have to make your question as clear as possible and give as much info as possible in a readable manner. It is a fact, as your question stands now, you will most definitely get an answer :-). +1 for the update Commented Nov 21, 2014 at 9:40

3 Answers 3

1

You are so very close here, you can almost smell it. Here is how you would complete your code:

You already have all your child terms stored in an array called $children. To display these, you simply need a foreach loop. Something like this will do the trick. (You can just extend on this, simply do a var_dump or print_r to get the available objects you can use)

foreach ( $children as $child ) {
    echo $child->name; //very basic, display term name
}

If you need to get a link to the term, make use get_term_link

2
  • Thanks, i've copied the foreach from the downloads page template which shows parent categories, and changed ($categories as $category) to ($children as $child), this seems to have worked, just testing now.
    – Adrian
    Commented Nov 21, 2014 at 9:58
  • Great, glad to hear it works. Enjoy :-) Commented Nov 21, 2014 at 10:02
1

Thanks again Pieter, been working on this for 2 days... Full code for anyone else who may need it in future:

<?php 
$term = get_queried_object();

$children = get_terms( $term->taxonomy, array(
'parent'    => $term->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) { // get_terms will return false if tax does not exist or term wasn't found.

foreach ( $children as $child ) {
          $imgurl = z_taxonomy_image_url($category->term_id); 
          echo '<li><img src="'. $imgurl . '"/><a href="' . esc_attr(get_term_link($child, 'downloadcats')) . '" rel="bookmark">' . $child->name . '' . '' . $child->description . '</a></li>'; 
}
} else {
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <li><?php the_post_thumbnail('post-image'); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php endif; ?>
<?php } ?>
0

This is a further clean up of the above solution to make it more generic...

<?php 
$term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent'    => $term->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) { ?>
// this category has a child category
<?php } else { ?>
// this category doesn't have a child category
<?php } ?>  

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.