If you are just getting started and wondering what is a Post Loop in WordPress development, then this post will be a help. The loop can be a little confusing at first so we have decided to convert it into a visual model infographic to help explain what goes on, line per line. Please feel free to share.Teal-Grey-Photo-Cycle-Diagram-Chart
When building your WordPress websites have you ever wanted to display recent posts from a specific category without going under the hood and editing the theme files? Diving into the code isn’t for everyone especially if you only want to implement this on a small area of the website such as a widget area. In this post, you are going to learn how to achieve this quickly and easily.
When showing recent posts in WordPress, this can usually be achieved using the recent posts widget, however, this does not allow you to target in on specific categories. There are third party plugins out there, but sometimes its good to be able to implement your own snippets of PHP without using a multitude of plugins for each task. Just use one plugin and implement your own code within it.
PHP Code Widget
For this, to work a customized text widget called PHP Code Widget by Otto needs to be installed. It looks very similar to a text widget but accepts PHP which an ordinary text widget can’t do. Place this widget in any widget area available for the installed theme.
Implementing The Loop
Once the PHP widget is in place, the following code needs to be pasted into it. This code is a basic WordPress Loop that runs through all the posts and identifies the category for each post. The title and link are then displayed.
<?php $catquery = new WP_Query( 'cat=11&posts_per_page=3' ); ?> <ul> <?php while($catquery->have_posts()) : $catquery->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> <?php wp_reset_postdata();?>
Category ID and Amount of Posts
The only areas of code that need amending are the category id and number of posts to display within the widget. In this case, three posts will be displayed. In order to identify the category number, head over to the posts category tab from within the dashboard and hover the mouse over a category. The ID will be displayed in the URL as shown below.
In the bottom left-hand corner of the screen, a URL will be displayed. The news category for this website is ’11’. This number will be different for every website. Once implemented, head over to the front end of the website and the latest three news category post titles show display like below. The only other difference here is the choice of CSS such as color, fonts, and arrows which will need to be added separately.
In this post, we learned how to manually add PHP and display recent posts from a specific category in a WordPress widget without going into the theme files. By doing this you can add PHP without worrying about messing the theme up. This is also good for small fixes and will help people learning to code grasp some important WordPress PHP fundamentals such as ‘The Loop’.