How to show posts from a specific category anywhere on your website
Often and often I’m asked by my clients for better ways to organize the information on their blogs. The classic listing of recent posts and some widgets in the sidebar just don’t make it. Readers want better organized information. The best way to provide it is to arrange posts in categories in a smart way, and then list them separated on your website.
You can achieve this in a simple way directly editing the template file your want. Even if it is index.php, or page.php, single.php, sdebar.php, you can list posts from a category from its category ID. First of all, look in your wordpress admin panel and get the ID of the category you want to display your posts.
Once you have the ID, change the followiing code accordingly and add it anywhere you want in your template files.
$cat_id = '12'; // Your category ID
$posts = get_posts('cat='. $cat_id .'&numberposts=5');
foreach($posts as $article) {
echo $article->post_title;
echo $article->post_content;
}
Here is a list with $article object variables:
- ID
- post_author
- post_date
- post_date_gmt
- post_content
- post_title
- post_category
- post_excerpt
- post_status
- comment_status
- ping_status
- post_password
- post_name
- to_ping
- pinged
- post_modified
- post_modified_gmt
- post_content_filtered
- post_parent
- guid
- menu_order
- post_type
- post_mime_type
- comment_count
Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the item, I will receive an affiliate commission
Leave a Reply