My code returns category name of default post type "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!
My code returns category name of default post type "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!
Share Improve this question asked Oct 23, 2020 at 7:27 JurajJuraj 112 bronze badges 1- Are you sure you want the category, not the post type name? – Rup Commented Oct 23, 2020 at 11:11
1 Answer
Reset to default 0The following code should return you the correct category names, however I am not clear what you're actually trying to achieve. I think you're using the foreach loop in wrong way. Are you trying to add all the category names as css classes for the div or something else?
<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_the_category ( $post->ID );
foreach ( $categories as $cat ) : ?>
<div class="col-lg-4 col-md-6 col-12 port-item mb-30 <?php echo esc_attr( $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>