WordPress.org

Ready to get started?Download WordPress

Forums

Custom taxonomy terms in the sidebar of a post (3 posts)

  1. design_dolphin
    Member
    Posted 4 years ago #

    Here is a bit of code which shows the terms of a custom taxonomy in the sidebar of a single post.

    If you want more than one custom taxonomy shown just change the code accordingly. The same goes if you want to show the custom taxonomy for more than one (parent) category for a single post. Please also read the documentation of the second bit of code in functions.php.

    Depending on your Permalink structure you may have to change the $url variable to suit your needs.

    Code subject to review.

    <?php
    	echo '<h4>Tags(s)</h4><ul>';
    
    	if ( in_category( 'category name 1' ) ||  post_is_in_descendant_category( 1 )) {
    		$posttags = get_terms('a custom taxonomy name');
    		foreach ($posttags as $posttag) {
    			$url = attribute_escape(get_bloginfo('url') . '/' . $posttag->taxonomy . '/' . $posttag->slug);
    			$list_tags = '<li><a href="' . $url . '" title="' . $posttag->name . '">' . $posttag->name . '</a></li>';
    			echo $list_tags;
    		}
    
    	}
    
    	elseif ( in_category( 'category name 2' ) ||  post_is_in_descendant_category( 2 )) {
    		$posttags = get_terms('another custom taxonomy name');
    		foreach ($posttags as $posttag) {
    			$url = attribute_escape(get_bloginfo('url') . '/' . $posttag->taxonomy . '/' . $posttag->slug);
    			$list_tags = '<li><a href="' . $url . '" title="' . $posttag->name . '">' . $posttag->name . '</a></li>';
    			echo $list_tags;
    		}
    
    	} else {
    		echo '<p>There are no tags for this post.</p>';
    	}
    
    	echo '</ul>';
    	?>

    You will also need this bit of code in the functions.php of your theme. This will allow you to enter the parent category in the sidebar code, and it should fetch the children categories automatically:

    /**
     * Tests if any of a post's assigned categories are descendants of target categories
     *
     * @param int|array $cats The target categories. Integer ID or array of integer IDs
     * @param int|object $_post The post. Omit to test the current post in the Loop or main query
     * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
     * @see get_term_by() You can get a category by name or slug, then pass ID to this function
     * @uses get_term_children() Passes $cats
     * @uses in_category() Passes $_post (can be empty)
     * @version 2.7
     * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
     */
    function post_is_in_descendant_category( $cats, $_post = null )
    {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category');
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }

    What I still want to figure out is how to get the custom taxonomy names automatically. This should then be able to be combined with getting the category name automatically with get_categories() for example. Well, that's the idea anyway.

    Hope it helps. :-)

  2. design_dolphin
    Member
    Posted 4 years ago #

    My bad. Correction to the code:

    echo '<p>There are no tags for this post.</p>';

    Should be:

    echo '<li>There are no tags for this post.</li>';

    It still W3C validated though. Odd, something to keep in mind. W3C validator might not spot the extra p tags as wrong within ul if li is missing.

  3. design_dolphin
    Member
    Posted 4 years ago #

    It still W3C validated though. Odd, something to keep in mind. W3C validator might not spot the extra p tags as wrong within ul if li is missing.

    Validated the code with the tags shown, but forgot to check when no tags were shown.

    @moderators:
    Any change of cleaning this topic?
    Many thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.