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!
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', ), ); }