December 14

Show Recent Posts From Selected Categories in Sidebar Without Plugin

WordPress has a built in widget to display recent posts but it doesn’t really offer much option. There are plugins that let you do that too but since we are already using the PHP code widget and would rather not install another plugin, we just use this code in a text widget instead.

<?php
$recent_posts = wp_get_recent_posts( array(
'numberposts' =>'5',
'post_status' => 'publish',
'category__in' => array(3, 8, 24, 25, 30, 26, 29, 31, 23, 13, 10, 11, 5, 6, 7)) );
foreach( $recent_posts as $recent ){ ?>
<li><a href="<?php echo get_permalink($recent['ID']); ?>" title="Look <?php echo $recent['post_title']; ?>">
<?php echo $recent['post_title'] ?></a></li>
<?php } ?>

You will have to edit the categories though. In our case, we have a lot of sub-categories in our knowledge base and that’s the reason for the long list of numbers inside the category__in array. You can delete the entire list and replace with the category IDs you want to show.

The other thing you might want to edit is numberposts. We have set ours to 5 latest posts.


Tags


You may also like

Force cPanel Password Reset

Force cPanel Password Reset
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get in touch

Name*
Email*
Message
0 of 350