Here’s how I’d get the 20 (at most) most recent posts in the category.php
page:
<?php
$max_posts = 20;
$cat_ID = get_cat_ID ( single_cat_title( '', false ) );
$args = array(
'category' => $cat_ID,
'numberposts' => $max_posts,
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) {
setup_postdata( $post );
?>
<!-- add your HTML here, including the_title(), the_excerpt(), etc -->
<?php
}
?>
The code uses get_cat_ID()
and single_cat_title()
to get the ID of the current category. I’ve done some (very) minimal testing to make sure that it at least excludes posts that aren’t in the current category.
Related Posts:
- Force category choice before creating new post?
- How to remove category from wordpress url?
- How to create a custom url for a category [closed]
- How to get primary category name in WordPress new version
- Rearranging posts based on categories
- How to create category index table out of wordpress site or on other website?
- Custom Nav Walker menu – Display category count
- Multisite blog converting categories to subdomains
- Redirecting specific category to subdomain
- How to make all posts in a category sticky?
- WordPress: category page not for post’s
- WordPress check if user is logged in from non wordpress page
- Automatically assign posts by author to a specific category
- How to show empty category in admin menus search
- Categories and Tags returning 404 on the sidebar when using ugly permalinks
- How do I remove the tags from wp_list_categories?
- How to group the loop posts in a particular by author?
- How to display get_the_category of post showing only CHILD of category “X”
- Get ID of current taxonomy in register_rest_field
- Custom page for WooCommerce’s /product-category/
- Add Class While on Current Post; wp_list_categories
- get all sub categories without specify any category
- How to use wp_category_checklist()?
- Get the name an the description of a link category
- Display child categories in WooCommerce
- WooCommerce duplicate sub categories and product slugs
- Check child/parent categories if exists
- How to run select query of post with category and tags for API?
- WordPress Genesis not excluding category from blog
- How to show only one category in breadcrumb navigation
- Trying to only display 1 category using get_posts
- How to display singular form of category name?
- Populate dropdown based on selection of parent category
- Select posts by name and category per REST API
- Categories for each brands
- Custom User-Created RSS Feed
- query cat orderby and order not working
- Permalinks: display just one kind of wp category in the posts url
- WooCommerce – How to show specific category first-rest of products-specific category last
- Multiple URL’s based on category for one post
- Get value of custom field in category admin
- WordPress category check
- Show category name, and last 5 post titles below category name?
- get_post_meta not working on category.php
- How can I improve this taxonomy treating approach?
- How to show more than 5 posts of the category?
- Can’t delete a category in WordPress
- Dropdown Categories used in form shows no results when all is selected
- Nicer URL for viewing category of posts?
- Get Categories Where Taxonomy Equals ‘n’
- Odd results in get_the_terms vs wp_get_post_terms
- Redirect Changed Permalink on wordpress
- Grandchild Term Things Grandfather Term is It’s Parent
- get full category structure by post id
- Home Page Template – Specific Category
- Remove Archive | from Categories
- Using categories with pages
- Show next post in same category but start from the latest
- List all posts of certain category which share some tags
- Search form options group categories
- Only first category!
- How could I write a get_categories_by_year() function?
- How to add icon over specific thumb
- Can I define multiple paginations on a single page?
- Echo Category Nicename
- Explode() expects a string
- How to add meta description, keywords, custom title to a category template
- User level categories
- Exclude category from drop down list form
- how to remove alphabetical order of category in WordPress?
- How to exit out of delete_categories and stop action?
- Programmatically Create Category and sub Category and sub sub Category
- Parent Category and Subcategory default text generated with category
- How to create a table with counts of category combinations?
- How to get the_category(‘, ‘) with link “title” attribute
- product_cat image url from database
- How to test if the post is in a primary category
- How to get the term id inside the start lvl method of category walker?
- Multiple if statements with else for get_the_category [closed]
- Most efficient way to display current post subcategories?
- How to display the past events in one of the category?
- How to Query the Top 5 Posts of a specific category?
- How to add a post with new Taxonomy without assigning to default category?
- Query post only from categories that have subcategories
- How to get the link of all categories
- Using wp_list_pages to create 2 lists of pages and include and exclude some of them depending on their category
- How to create default categories in new installs?
- If I change the taxonomy of a post will it get a new time stamp?
- Check if value is a custom taxonomy category name
- Tracking multiple tags or categories
- 2 loops in archive.php (one for each category)
- How to load jquery tag-it plugin into admin?
- Include Category Description in search result
- get latest post in category date
- How to make wp dropdown category load with selected option on top?
- list taxonomy based on taxonomy
- How can I get rid of the the category suffix–NOT “prefix” (i.e. foo-1, foo-2, etc)
- Random post order into Category Pages
- Why Pagination is not working on Category.php
- Listing categories on a wordpress page