• Hey guys,

    searching for a way to display the category names of my custom post type. It would be nice to display categories and their permalinks, but the get_the_category(); function only echo an array. I am surprised that there is no simple function. Is there a smart way?

    <?php
    
    $args = array(
    	'posts_per_page'	=>	-1,
    	'post_type' => 'myposttype',
    
    );
    
    $myposttype_array = new WP_Query( $args );
    if( $myposttype_array->have_posts() ) {
    
    	$counter = 0;
    
    	while( $myposttype_array->have_posts() ) {
    		$myposttype_array->the_post();
    ?>
    
    			<?php echo get_the_category();> 
    
    			<h1><?php the_title() ?></h1>
    
    			<?php //the_excerpt(); ?>
    			<?php the_content(); ?>
    
    <?php
    		$counter++;
    		}
    	}
    	else {
    		echo 'nothing.';
    		}
    ?>

    Thanks in advance,
    Christian

Viewing 1 replies (of 1 total)
  • Thread Starter p2or

    (@p2or)

    Solved. To get the categories of a custom post type you have call get_the_terms() and setup the registered category name like this:

    <?php
    $terms = get_the_terms( $post->ID , 'myposttype_category' );
    foreach ( $terms as $term ) {
      echo $term->name;
    }
    ?>

    Best,
    Christian

Viewing 1 replies (of 1 total)
  • The topic ‘Display category names of custom post type’ is closed to new replies.