php – Add Category names to post lists of custom post type

Question

im beginner at codes, I have some code to show posts of “Custom Post Type” and it’s working and it shows post titles, I want to add a category name to it too. ” I tried <?php echo get_the_category(); ?> but its not working. Thank you “

<?php 
// Set the arguments for the query
$args = array( 
  'numberposts'     => -1, // -1 is for all
  'post_type'       => 'custom post type name', // or 'post', 'page'
  'orderby'         => 'title', // or 'date', 'rand'
  'order'       => 'ASC', // or 'DESC'
  // 'category'         => $category_id,
  // 'exclude'      => get_the_ID()
);

// Get the posts
$myposts = get_posts($args);
// If there are posts
if($myposts):
  // Loop the posts
  foreach ($myposts as $mypost):
?>
  <div class="row">
    <!-- Content -->
    <div class="col-xs-9">
      <a href="<?php echo get_permalink($mypost->ID); ?>"> <?php echo get_the_title($mypost->ID); ?></a>
    </div>
  </div>

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

0
Big Smile 1 month 2022-10-23T15:09:13-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse