• Resolved c.e.macht

    (@cemacht)


    I have created a hierarchical custom taxonomy ‘page_category’ (similar to post category) through my functions.php and would like to use conditional if-statements to display different images in my header.

    My problem is however, that I don’t know how to access these new taxonomies. Apparently in_category() only works with the standard post category, I would probably need a new function like in_page_category(). How do I write this?

    A very similar problem has been discussed in this topic I believe though, that has_tags() will not be of help with my hierarchical taxonomies.

    Any help is greatly appreciated!

    // pointers on custom taxonomies from Justin Tadlock
    A refresher on custom taxonomies
    Custom Taxonomies in WP 2.8

Viewing 1 replies (of 1 total)
  • Thread Starter c.e.macht

    (@cemacht)

    So.. as always, the trick is to ask “Why does this work how it does?” and look up the root. In this case through cross-reference searching where in_category() is defined, copying that function to functions.php and replacing the standard $category with your custom $page-category.

    You’ll find in_category in wp-includes/category-template.php, l. 237.

    /**
     * Check if the current post in within any of the given categories.
     *
     * The given categories are checked against the post's categories' term_ids, names and slugs.
     * Categories given as integers will only be checked against the post's categories' term_ids.
     *
     * Prior to v2.5 of WordPress, category names were not supported.
     * Prior to v2.7, category slugs were not supported.
     * Prior to v2.7, only one category could be compared: in_category( $single_category ).
     * Prior to v2.7, this function could only be used in the WordPress Loop.
     * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
     *
     * @since 1.2.0
     *
     * @uses is_object_in_term()
     *
     * @param int|string|array $category. Category ID, name or slug, or array of said.
     * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
     * @return bool True if the current post is in any of the given categories.
     */
    function in_category( $category, $_post = null ) {
    	if ( empty( $category ) )
    		return false;
    
    	if ( $_post ) {
    		$_post = get_post( $_post );
    	} else {
    		$_post =& $GLOBALS['post'];
    	}
    
    	if ( !$_post )
    		return false;
    
    	$r = is_object_in_term( $_post->ID, 'category', $category );
    	if ( is_wp_error( $r ) )
    		return false;
    	return $r;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Hierarchical Custom Taxonomies and Conditionals’ is closed to new replies.