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:
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.