WordPress: Display latest posts from a particular category in the sidebars of specific pages

After spending hours trying to figure out a way to display the latest posts from a certain category in WordPress on specific pages, I have finally found a solution!

First of all, it is important to note that the site I am working on is set up to display only posts from a particular category on specific pages.

The first thing I did was install the Exec-PHP plugin (which allows you to add php code to your sidebar widgets) and the Widget Logic plugin (which allows you to control which widgets show up on which pages’ sidebars).

Next, I added a “text” widget to my sidebar. I named the widget “Latest posts in Video.”

Then I pasted this code into the text area of the widget:

<?php
 global $cat;
 $recentPosts = new WP_Query();
 $recentPosts->query('showposts=5&cat=7');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
 <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>

I changed the category ID (in this case: “7″) to the category ID that I wanted to display on the Video page.

Next, in the Widget Logic field of the widget, I used this line of code to tell the widget which page to appear on:

is_category(7)

That is where I had been going wrong. I had been using is_page(26) (which is the page ID where I have Video posts being displayed), but WordPress sees that page as a category page and not as a page page (because of the fact that I had it set up to show only a certain category of posts), so once I changed the Widget Logic code to is_category, rather than is_page, my solution worked perfectly.

As always, let me know if you have any questions or issues with this, I would be happy to help.

Related Posts:

9 Responses to “WordPress: Display latest posts from a particular category in the sidebars of specific pages”

  1. Reza says:

    a big question! in this code, where I should replace or add my specific category?

    • Libby Fisher says:

      Hi Reza,

      In this piece of the code: $recentPosts->query(‘showposts=5&cat=7′);

      Replace the cat=7 with the category ID of the category that you want to display.

      Does that answer your question??

  2. Reza says:

    nop, I confused :(

    I did everything here and ” display only posts from a particular category on specific pages “, one by one, but it doesn’t work for me.

    it’s 2 days I’m searching to find a way to show latest post from specific category on the specific page, in sidebar.

    • Libby Fisher says:

      Is your site set up to show certain categories on certain pages? For instance – you have a page for all posts in Category 1, a page for all posts in category 2, etc.?

  3. Khandaker Limon says:

    Thank you very much Libby Fisher for this type of post . it’s working on me . i am very happy .

    Again great thanks . :)

  4. Ricky1990 says:

    Hi,
    Thanks for great post ! But it doesn’t show thumbnail. You should add functions to show thumb.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">