WordPress get_title from category

I was wondering how to get the title/img/content of some random article, but from specific category.

For an example:
I have 3 categories A,B and C and I have a image slider on my blog.
I want to show on the slider ONLY those articles who are in category A, not B and C.
How can I make that happen ? 🙂

Related posts

Leave a Reply

1 comment

  1. The below example should get you started. It basically calls the get_posts() function with some criteria.

    • Return 5 Posts
    • Random Order
    • From Specificed Category

    Then we run a foreach on the returned posts to do what we want. You don’t have to run a foreach, in the below example $rand_posts will hold an array post objects with which you can do what you want.

    You can take a look at the codex and change the arguments, criteria, to whatever you’d like.

    WordPress Codex – Get Posts

    <?php
        $cat_id = // Your category ID.
        $args = array('numberposts' => 5, 'orderby' => 'rand', category => $cat_id);
        $rand_posts = get_posts($args);
        foreach($rand_posts as $post) : ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            // Access all other post information here just like in a normal look. (Ex. the_content(), the_excerpt(), etc, etc
    <?php endforeach; ?>