<?php $cats = get_categories('child_of=".get_query_var("cat'));
foreach ($cats as $cat) :
$args = array(
'posts_per_page' => 3, // max number of post per category
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
echo '<h3>'.$cat->name.'</h3>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php /*general loop output; for instance: */ ?>
<a href="https://wordpress.stackexchange.com/questions/42272/<?php the_permalink() ?>"><?php the_title(); ?></a> <br />
<?php endwhile; ?>
<?php else :
echo 'No Posts for '.$cat->name;
endif;
wp_reset_query();
endforeach; ?>
use wp_reset_query()
; before endforeach for reset WP_Query.
Related Posts:
- How to show related posts by category
- Display all posts from specific categories on a page
- Query only Posts from Both of Two Category?
- Featured Posts for Category Pages
- How to make multiple sections in home pulling posts category wise?
- All categories displays the same posts
- WordPress displays post on subcategory only
- How to get posts ordered by using their categories?
- Getting posts from some categories plus some individual posts
- How to display particular categorie’s post which associated to specific user?
- Querying posts from current category, using a variable as array argument
- Need help writing loop to display posts by categories in separate divs
- Query post category & remove any post id
- How to pass multiple checkbox values to wordpress query?
- Change from all posts to specific categories post on main page?
- Sort Posts Alphabetically Based on Specific Category (Divi)
- How to add category to: ‘wp-admin/post-new.php’?
- Use category base slug in posts’ permalink
- the_author() returns empty string
- Setting Custom Sort Order of Posts within a Category
- Display the latest post from a category in a page
- How to show list of posts by author and category?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- How to display posts by current user/author in a custom page template?
- How to control which category will be picked for the slug of a post?
- Add default content to post (for specific category)
- Insert custom div between posts
- Display most recent post in category instead of archive?
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- How to Display Post View Count
- get_post_type_archive_link(‘post’) returns current category archive
- Get user’s most published categories
- Conditional that checks if a current category has any subcategory with posts
- How to set post expiration date and time and move the page to archive after expiration [closed]
- How would I get 1 latest post from a query for 5 posts?
- Exclude posts with empty post_content in wp_query
- Exclude posts from featuring
- Articles are text-only in any “Category” instead of HTML
- How To Remove Certain category Posts From the Blog Page
- Add icon/badge next to posttitle of specific category
- Get current category id from post page wordpress
- Saving category to a post, before publishing the post
- is there a way to show the the post title after the image?
- Exclude category from
- How to exclude latest x posts from a paginated query?
- WordPress Multisite – Create Default Post and New Category On New Site Install
- Disable sticky option for specific categories
- date issue with category post retrival
- Query Posts by Custom Field
- Conditionals if tags exist?
- How to get subcategories from category slug?
- Showing categories and subcategories with posts
- Change default category when I publish a post
- Posts – display all posts except a post by an ID
- Loop doesn’t exclude the specified category in home page
- Post Image not displaying in category view
- why the same code got different results when using query_posts in functions.php and index.php
- Parent category / child category posts
- WP_Query: Show 10 posts in date order, first three random
- Random posts that always include a particular post?
- Unable to differentiate between two categories under custom post type on single.php
- Exclude post category in a blog page
- WP_Query of Category Not Showing First Post
- How to make posts being uncategorized
- How to display a single(current) post from a category
- displaying the categories post
- What’s a theme that properly handles previews of different types of posts?
- Change content off every sixth element
- How to add content above footer in posts from specific category
- WordPress – When visiting a `single post/ single.php` my category menu item link becomes active
- Show posts from two specific category in WP_Query
- How to create loop of posts except post ID defined via ACF field
- How to Create another Page Category like the “Post” and “Pages”
- How to create a button to filter posts (list) by meta value
- Is there a way to save different data when USER interacts with the same POST?
- Display Specific Categories posts on the home page
- show only one category and filter by tag
- Code to display random ordered posts in Categories across pages [duplicate]
- AJAX load more posts not using correct category and repeating the same few posts
- Category posts show on local install, they do not show on live server
- How can I exclude only a specific sub category post from category display?
- how to print total number of posts filtered by category?
- How to correctly escape data
- Sort Events by Venue Title – Sort Post set by related post ids
- Add category information beneath post?
- Get posts by category or author
- Newly created category does not show category
- Show a Category Specific Info Box
- How to get all post titles starting with numbers and symbols?
- How to inject a post within a loop
- Display the first post’s comments of category in comments.php template
- How to filter categories of a post
- List posts related to category on a div [closed]
- how to show all post with its contents
- wordpress show category link instead of post link [closed]
- CSS – Change height of Storefront / WooCommerce product categories, but not products
- query_posts different amount of posts per page [duplicate]
- How to make category for word post_content
- I have a website issue I am trying to resolve
- How to sort posts alphabetically based on a specific parent category