简体   繁体   English

Wordpress - 获取自定义帖子类型的类别名称

[英]Wordpress - get category name of custom post type

My code returns category name of default post type "post":我的代码返回默认帖子类型“post”的类别名称:

<div class="row masonary-wrap">

<?php 
$args = array(
    'post_type' => 'project',
    'post_status' => 'publish',
    'posts_per_page' => '6'
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();

        $categories = get_categories( $args ); 

        foreach ( $categories as $cat ) : ?>

            <div class="col-lg-4 col-md-6 col-12 port-item mb-30 <?php echo esc_attr( $cat->cat_name ); ?>">    
                <div class="project">
                    <div class="proj-img">
                        <div class="proj-overlay">
                            <h5><?php the_title(); ?></h5>                          
                        </div>
                    </div>
                </div>
            </div>

        <?php
        endforeach;
    endwhile;
    wp_reset_postdata();
endif; ?>

</div>

I need to display category name or better slug of custom post type "project", not of default posts!我需要显示类别名称或更好的自定义帖子类型“项目”,而不是默认帖子!

you can do something like that.你可以做类似的事情。 please check the below code请检查下面的代码

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

thanks,谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM