wp query - Displaying the category name of a custom post type

admin2025-01-07  3

So I have a custom query, in which I'm displaying some results posts of a custom post type called "staff". This custom post type is linked to a custom taxonomy called "department". I am able to display results, but I am unable to print the category that's linked to each post.

This is my code:

        <?php
          $args = array(
            'post_type' => 'staff', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC',
            'tax_query' => array(
              array(
                'taxonomy' => 'departments',
                'field' => 'slug',
                'terms' => 'board'
              )
            )
          );
          $loop = new WP_Query( $args );
        ?>

        <?php if( $loop->have_posts() ): ?>

            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <p class="text-center name"><?php the_title(); ?></p>
                    <?php the_category(' '); ?>

            <?php endwhile; ?>        

        <?php endif; ?>

I think the problem is that I'm using but I'm not sure.

Any ideas what could be wrong?

Thanks!

So I have a custom query, in which I'm displaying some results posts of a custom post type called "staff". This custom post type is linked to a custom taxonomy called "department". I am able to display results, but I am unable to print the category that's linked to each post.

This is my code:

        <?php
          $args = array(
            'post_type' => 'staff', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC',
            'tax_query' => array(
              array(
                'taxonomy' => 'departments',
                'field' => 'slug',
                'terms' => 'board'
              )
            )
          );
          $loop = new WP_Query( $args );
        ?>

        <?php if( $loop->have_posts() ): ?>

            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <p class="text-center name"><?php the_title(); ?></p>
                    <?php the_category(' '); ?>

            <?php endwhile; ?>        

        <?php endif; ?>

I think the problem is that I'm using but I'm not sure.

Any ideas what could be wrong?

Thanks!

Share Improve this question edited May 22, 2015 at 3:28 Johann asked May 22, 2015 at 3:08 JohannJohann 8674 gold badges14 silver badges31 bronze badges 1
  • Custom post types don't have categories. It seems that you want to retrieve the custom terms of the "departments" taxonomy. However your query is already set to only poll the "board" department so there's no point to retrieve it. If you do want to retrieve it anyway. Look at the function get_the_terms codex.wordpress.org/Function_Reference/get_the_terms – gdaniel Commented May 22, 2015 at 3:39
Add a comment  | 

4 Answers 4

Reset to default 15

So this is what I needed:

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

use terms like this :

$terms = get_the_terms($post->ID, 'Enter_your_taxonomy_here' );
if ($terms && ! is_wp_error($terms)) :
    $tslugs_arr = array();
    foreach ($terms as $term) {
        $tslugs_arr[] = $term->slug;
    }
    $terms_slug_str = join( " ", $tslugs_arr);
endif;
echo $terms_slug_str;

In case if someone looking for this in 2019. With this you are getting CUSTOM POST TYPE name with URL

$terms = wp_get_post_terms( $post->ID, 'PLACE-HERE-YOUR-TAXONOMY');
foreach ( $terms as $term ) {
  $term_link = get_term_link( $term );
  echo '<a href="' . $term_link . '">' . $term->name . '</a>' . ' ';
}

$categories = get_terms(array( 'taxonomy' => 'course_categories', 'hide_empty' => false, ));

        $output .= '<select name="category">';
        $output .= '<option value="">Select Category</option>';
        foreach ($categories as $category) {
            
            $output .= '<option value="' . $category->slug . '">' . $category->name . '</option>';
            
        }

if (!empty($category_data)) { // If a category is selected, filter by that category $post['tax_query'] = array( array( 'taxonomy' => 'course_categories', 'field' => 'slug', 'terms' => $category_data, 'operator' => 'IN', ), ); }

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263097a864.html

最新回复(0)