0
votes

I've made a custom loop which displays all posts and custom post type posts on one page.

I am looking to show under the title either the category of the post, or the post type "Case Study", depending on which section the post belongs to. There are no categories in my custom post type "Case Study".

The way the code is written here, it will show the category of the post and nothing if the post belongs to the custom post type "Case Study". I am having trouble setting up the conditional statement to show either a category name or the post type name.

<?php $all_query = new WP_Query(array('post_type'=>array( 'post', 'case-study'),     
'post_status'=>'publish', 'posts_per_page'=>-1)); ?>

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

<?php the_title()?>
<?php the_category(', '); ?>
<?php the_excerpt();?>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

**EDIT I have added this code (to replace the_category), but I realize it is sloppy and there is probably a better way. Any input or pointers is greatly appreciated!

<?php if (in_category('blog')) {  ?>
<?php the_category(', '); ?>
<?php } elseif  (in_category('pr')){  ?>
<?php the_category(', '); ?>
<?php } else {  ?>
<a href="<?php echo site_url(); ?>/case-studies">Case Study</a>
<?php } ; ?>
1

1 Answers

0
votes

What about this? I think get_post_type() will use the current post in the loop. If not you need to pass either the ID or the WP_Post object into that function.

if( get_post_type() == 'post'){
    the_category(', ');
}elseif( get_post_type() == 'case-study'){
    //your code
}