admin管理员组

文章数量:1410724

I have a custom post type that I'm importing into the front page of the site with WP_Query(), but I can't seem to get the custom taxonomy to show.

I've placed a div in the HTML and inside this I'm calling the_category(), but nothing is being outputted to the front end.

The taxonomy is showing in the back end and I've added a custom taxonomy (category) to all of the three posts I want to show.

<?php 

    $homePageNews = new WP_Query(array(
        'posts_per_page' => 3,
        'post_type'=> 'news'
    ));

    while($homePageNews->have_posts()){
        $homePageNews->the_post(); ?>

            <article class="article top-article lastest-top-article">
            <?php the_post_thumbnail('post-thumbnail', 
                ['class' => 'image latest-top-image hover-class']); ?>

                <div class="cat"><?php the_category( ', ' ); ?></div>

                <div class="content-wrapper content-wrapper-mobile-padding">
                    <h3 class="td no-bottom-margin hover-class"><a class="top-latest-heading" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <hr>
                    <p><?php the_content(); ?></h3></p>
                </div>
            </article>


<?php } ?>

<?php  wp_reset_postdata(); ?>

I have a custom post type that I'm importing into the front page of the site with WP_Query(), but I can't seem to get the custom taxonomy to show.

I've placed a div in the HTML and inside this I'm calling the_category(), but nothing is being outputted to the front end.

The taxonomy is showing in the back end and I've added a custom taxonomy (category) to all of the three posts I want to show.

<?php 

    $homePageNews = new WP_Query(array(
        'posts_per_page' => 3,
        'post_type'=> 'news'
    ));

    while($homePageNews->have_posts()){
        $homePageNews->the_post(); ?>

            <article class="article top-article lastest-top-article">
            <?php the_post_thumbnail('post-thumbnail', 
                ['class' => 'image latest-top-image hover-class']); ?>

                <div class="cat"><?php the_category( ', ' ); ?></div>

                <div class="content-wrapper content-wrapper-mobile-padding">
                    <h3 class="td no-bottom-margin hover-class"><a class="top-latest-heading" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <hr>
                    <p><?php the_content(); ?></h3></p>
                </div>
            </article>


<?php } ?>

<?php  wp_reset_postdata(); ?>
Share Improve this question asked Jan 28, 2020 at 3:27 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges 1
  • 1 does your custom post_type use a real 'custom' taxonomy or the generic taxonomy 'category'? developer.wordpress/themes/basics/… – Michael Commented Jan 28, 2020 at 3:50
Add a comment  | 

2 Answers 2

Reset to default 1

From your explanation above I understood that you want to show custom taxonomy you have registered for that post type.

the_category(); fetches the default category that comes with the default post.

For rendering custom taxonomy you need to add something like this:

$terms = get_the_terms( $post->ID , 'your custom taxonomy slug' );
foreach ( $terms as $term ) {
 echo $term->name;
}

If you want to get custom taxonomy comma separated listing than we can go with WordPress inbuilt function get_the_term_list()

get_the_term_list( $id, $taxonomy, $before, $sep, $after )
$id        : Post ID
$taxonomy  : Name of taxonomy
$before    : Leading text
$sep       : String to separate tags
$after     : Trailing text

for example,
<?php echo get_the_term_list( $post->ID, 'people', 'People: ', ', ' ); ?>

or we can go with

$term_obj_list = get_the_terms( $post->ID, 'taxonomy' );
$terms_string = join(', ', wp_list_pluck($term_obj_list, 'name'));

本文标签: wp queryCustom Taxonomy Not Showing in FrontEnd When Outputting a Custom Post Type with WPQuery()