Trying to determine in the loop if a post has a custom

Trying to determine in the loop if a post has a custom press type, and if it does to echo the category name....

Right now I have:

					$category = get_the_category();
						echo '<a href="' . esc_url(get_category_link($category[0]->term_id )) . '">' . esc_attr($category[0]->cat_name) . '</a>';

It is displaying the name of the custom post type, but if it is a custom post type, I'd like it to echo the category, not just the post type....

  • Adam Czajczyk

    Hello @atracksler,

    I hope you're well today and thank you for your question!

    I'm not sure if the code you gave is the proper one, are you sure of this? It seems for me like a code that displays the category name as a link to a category. Apart from this, am I correct saying that you'd like to display the name of a custom category (taxonomy)?

    I think it will be good if I include @Hoang Ngo here who should be able to give you some helpful tips on this as a CustomPress plugin developer.

    Please keep an eye on this thread for further information!

    Best regards,

  • Adam Czajczyk

    Hey atracksler,

    Thanks for your feedback.

    To display custom categories you'll need a bit different code than to display "standard" categories. Below is an example of a shortcode function that displays all the categories for our CoursePress plugin (which are custom categries):

    function wpmu_list_cp_categories_shortcode() {
    	$wpmu_cp_categories = '<ul>';
    	$args = array (
    		'title_li' => '',
    		'hide_empty' => 0,
    		'depth' => -1,
    		'taxonomy' => 'course_category'
        foreach (get_categories($args) as $cat) {
                $wpmu_cp_categories .= '<li>/><a href="'.get_term_link($cat->slug,'course_category').'">'.$cat->cat_name.'</a></li>';
        $wpmu_cp_categories .= '</ul>';
    	return $wpmu_cp_categories;
    add_shortcode('course_category_list', 'wpmu_list_cp_categories_shortcode');

    As you can see there are two crucial factors:

    - you need to provide the "get_categories()" function with a taxonomy name (which is "course_category" here for CP categories)
    - you're using "get_term_link()" with a "category slug" with a parameter.

    I hope this will give you a good starting point here.

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.