WordPress.org

Ready to get started?Download WordPress

Forums

get_the_category_tax (2 posts)

  1. merlinox
    Member
    Posted 4 years ago #

    I write a custom function 'cause get_the_category, and all others category functions, don't maybe used for custom taxonomy.

    I did it:

    /**
     * Retrieve post categories.
     *
     * @since 0.71
     * @uses $post
     *
     * @param int $id Optional, default to current post ID. The post ID.
     * @param string $taxonomy Optional, default to 'category' type. The taxonomy type.
     * @return array
     */
    function get_the_category_tax( $id = false, $taxonomy = 'category' ) {
    	global $post;
    
    	$id = (int) $id;
    	if ( !$id )
    		$id = (int) $post->ID;
    
    	$categories = get_object_term_cache( $id, $taxonomy );
    	if ( false === $categories ) {
    		$categories = wp_get_object_terms( $id, $taxonomy );
    		wp_cache_add($id, $categories, $taxonomy . '_relationships');
    	}
    
    	if ( !empty( $categories ) )
    		usort( $categories, '_usort_terms_by_name' );
    	else
    		$categories = array();
    
    	foreach ( (array) array_keys( $categories ) as $key ) {
    		_make_cat_compat( $categories[$key] );
    	}
    
    	return $categories;
    }
  2. merlinox
    Member
    Posted 4 years ago #

    ps: the better thing maybe replace original get_the_category function, with this. $taxonomy param is optional, and would not have entered the regression errors.

Topic Closed

This topic has been closed to new replies.

About this Topic