WordPress.org

Ready to get started?Download WordPress

Forums

Custom Taxonomy Children (4 posts)

  1. SeanD
    Member
    Posted 4 years ago #

    Hello - I am trying to accomplish the following:

    I'm trying to do something that was fairly easy to achieve with Categories but proves challenging when using Custom Taxonomies.

    When displaying a Taxonomy page, I want to detect whether the taxonomy classification has children.

    If it does have children, I would like to display just the child classifications and their descriptions. I do not want posts to be listed here.

    If it does not have children, then I would like to display the post listing.

    Previously, using categories, I was achieving this using the following code:

    <?php global $ancestor; // Declare global ancestor variable ?>
    <?php
    $categories = get_categories('child_of=14&order=DESC');
    foreach ($categories as $cat) {
    	if ($cat->cat_ID != 1) { // If category is not uncategorized ...
    	if (cat_is_ancestor_of($ancestor, intval($cat->cat_ID)) == false) { // .. and if the previous category in the loop wasn't the ancestor of this one ... ?>
    		<div class="entries">
     		<div class="entry">
            <h2 class="bold"><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo ($cat->cat_name); ?></a></h2>
    		<?php echo category_description($cat->cat_ID);  ?>
            <p><a href="<?php echo get_category_link($cat->cat_ID); ?>">Read more</a>&nbsp;&raquo;</p>
    		</div>
            </div>
    		<?php
    		$ancestor = intval($cat->cat_ID); }; // make this category the new ancestor
    		};
    	};
    ?>

    Any help would be appreciated!

  2. chymera00
    Member
    Posted 3 years ago #

    I'm also having the same problem

  3. antmeeks
    Member
    Posted 3 years ago #

    Has anyone solved this?

    I want to do the exact same thing...

  4. Omnilogic
    Member
    Posted 3 years ago #

    This might work for you.

    $taxonomy_term_id = $wp_query->queried_object_id;
    $taxonomy = 'your-taxonomy-name';
    $taxonomy_children = get_term_children($taxonomy_term_id, $taxonomy);
    if (empty($taxonomy_children)) {
    	echo 'this worked';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic