WordPress.org

Support

Support » Requests and Feedback » get_the_category_tax

get_the_category_tax

  • merlinox
    Member

    @merlinox

    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;
    }

Viewing 1 replies (of 1 total)
  • merlinox
    Member

    @merlinox

    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.

Viewing 1 replies (of 1 total)
  • The topic ‘get_the_category_tax’ is closed to new replies.