how to show a category from a CPT?

I have a custom post type called ‘seasons’ and in there i have 3 categories (summer,winter and spring.)

I want to show 10 random post from the winter category on a page.

Read More

Please help.

Related posts

Leave a Reply

2 comments

  1. WP_Query is the solution to your problem.

    Go through the available parameters. Just add a query and a loop in your page template and you are good to go.

    Ex.

    $args = array(
    'category_name' => 'winter',
    'orderby' => 'rand',
    'posts_per_page' => 10
    );
    
    $query = new WP_Query( $args );
    

    The loop starts here:

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

    and ends here:

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
  2. You need to add the custom post type in the $args array in the code above by Rohit:

    $args = array(
        'orderby' => 'rand',
        'posts_per_page' => 10,
        'post_type' => 'seasons', //ADD THIS
        'category_name' => 'winter'
        );