In the course of developing this blog, I run into a lot of challenges. I’m pretty picky, so that often means figuring out how to produce certain results. Henceforth, lots of googling, lots of potential solutions, and testing and tweaking until I’ve got things just right. For this tutorial, I’m going to take one such challenge and guide you through how to do it.
The challenge
Use PHP and a plugin to list on one post the posts from a specific category.
The plugin:
Insert PHP
The tutorial I used:
‘How to Display Recent Posts From A Specific Category In WordPress’
I used the second method on that page, displaying recent posts from a specific category using a code snippet. I didn’t really want to go into the WordPress theme files and mess with them, however, and just wanted to pull the posts from a couple different categories onto two different posts. To achieve this, I used the Insert PHP plugin to wrap the code provided by the tutorial in Insert PHP brackets. Then, of course, I modified that code to suit my needs. The code, along with commenting by me, follows:
What You Need to Know
1) Start by wrapping the code with the opening insert_php bracket.
2) Pay attention to cat=72! You need to update it to the category you want to use. Find out what your category’s id number is by looking under wp_terms in your WordPress database. Use the id for the category you want to pull the posts of. You can also specify the number of posts per page via this snippet: posts_per_page=5
.
3) I removed the list formatting from the original code. I didn’t want bullets or list formatting. The beauty of implementing the content this way is that you can format it the way you want pretty freely.
4) Important! Where it says <?php the_content(); ?>
, you can opt to only show the excerpt for each post by using <?php the_excerpt(); ?>
instead.
5) Lastly, close the code with the closing insert_php bracket.
[insert_php] $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?> <?php while($catquery->have_posts()) : $catquery->the_post(); ?> <br /> <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> <br /><br /> <?php the_excerpt(); ?> <?php endwhile; ?> <?php wp_reset_postdata(); [/insert_php]
The End Result
How your page ends up looking, however, is determined almost entirely on the theme you’re using as evidenced by the shot of the same page below (using a different theme.)
Now, It’s Your Turn
You are welcome to use this same method or visit the tutorial that I used for more ideas! I found their post pretty useful, and you may too.