Hi @Raffaele:
The function wp_get_object_terms(...)
can provide you with the information you need. I’ve written a has_category($post_id)
function that can be used in the loop like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php if (has_category($post->ID)): ?>
<p><?php the_title(); ?></p>
<?php endif; ?>
<?php endwhile; ?>
And here is the has_category()
function:
<?php
function has_category($post_id) {
$has_category = false;
$terms = wp_get_object_terms($post_id,'category');
if (is_array($terms)) {
foreach($terms as $index => $term)
if ($term->slug=='uncategorized')
unset($terms[$index]);
$has_category = (count($terms)>0);
}
return $has_category;
}
Note that my has_category()
function treats posts with the 'uncategorized'
category as having no category. There are probably more performant ways to accomplish this but what you see above should work.
-Mike
Related Posts:
- ajax for filtering posts by category in wordpress loops
- Get the children of the parent category
- How do I get the category URL from get_the_category?
- Default WP Gallery – show only galleries of a certain category
- Rearranging posts based on categories
- WP_Query not looking at child category
- Display all posts in category, with specific tag posts at top
- Make parent categories not selectable
- Display list of Sub-Categories and the posts they contain, within one main Category
- Remove child products from woocommerce category page [closed]
- Get a list of commas separated categories inside a loop
- Check if post belongs to any category
- How do I display the posts from a custom post type on a category.php page?
- How to group the loop posts in a particular by author?
- Can paginate_links() be customized for a specific category or tag?
- Exclude category from loop not working
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- Make parent category not selectable when it has child categories
- If newest post of category is newest post in general, skip first post of category
- Hide posts of a certain category unless logged in
- Why would in_category only return one post in a specific category?
- What’s the Simplest Way to Override/Rewrite the %category% Permalink Structure Tag?
- Is it possible to select and edit the way the most recent post from a certain category is displayed on the page?
- How to filter posts that belong to a specific category only if that is the only category
- How to Filter categories in the permalink structure
- How to display posts under a specific subcategory of a given category
- Taxonomy filter all children
- Categorising posts/pages into years published
- Displaying multiple loops based off of category
- Redirect category to url with /category
- Enter a variable in the ‘category_name’ parameter
- Automatically set default password to all posts in a specific category
- Second level subcategory
- how to define category loop last class in the third
- get_the_categories filter returns an empty array
- category filter doesn’t work in WPML
- Query posts from category based on a filter most favorited
- Remove product category placeholder image (Woocomerce)
- How to organize custom categories for filtering and selecting
- Multiple Loops on category.php with pagination on last loop
- Refine/Filter the Search Results by Category
- Pagination for ajax category filter
- How to hide category name
- Exclude current post from loop
- How to insert category list into post creation page, and retrieve chosen categories?
- Display single category name in woocommerce loop
- Show Posts from Single Category Not Working
- How to make the ‘delete’ button inactive on some categories?
- Pagination on category.php and tag.php not working
- Problem with different query loops (and “main loop”) on category template page!
- How to do paging in the loop?
- How to loop custom post_type from (a) specific category(ies)?
- Display all product tags associated with a specific product category
- Display posts of certain categories to specific user roles
- Simple way to style posts of a single category differently in the loop and in single
- How to hide some categories in dashboard
- Change post title if post has specific category
- Filter By Category Dropdown List With Custom Taxonomies
- Have posts that belong to multiple categories, exclude some categories from homepage
- Get the ID of the current post’s child category
- Problem with multiple loops in wordpress theme
- multiple values in an array for category__and does not work with WP_Query
- How can I filter posts by tag on the fly
- Display post thumbnail for specific category outside the loop
- List post only under the category, exclude child category content
- Change Default Content when Creating a Post based on Previous Category Choice
- Maintaining a separate posts page for certain categories and don’t show those on home page
- How to get posts using category slug in ClassiPress?
- Category sticky latest
- Show the number of the post
- How can I order all subcategories alphabetical independent of the parent categories?
- How to filter tags by category?
- Custom Taxonomy Isotope Grid – Filter Buttons Not Working
- Correct Hook/Filter to amend category choices on post edit page
- How to create a category loop on my blog (ACF Plugin)
- Displaying Category in sidebar post widget but not in the loop on home
- Querying posts from two different categories while looping inside another loop
- Loop doesn’t work
- ACF Gallery with Media Categories – how do display categories and sort
- show category name before first posts in each category
- Loop within category’s posts
- Display one post on category.php: Wrong featured image
- Loop through posts of CatA and store value of CatB in separate array
- How to add an empty entry to masonry?
- WordPress Numeric Pagination with Query String [duplicate]
- Loops in category description
- Several loop in search result
- Printing direct descendants of a category with WP_Query
- Category slug in in loop always the same?
- how to get number of posts in a category on a specific date
- Show posted on date only for posts in a certain category
- 2 loops in archive.php (one for each category)
- Detecting top parent category fails on home page
- Using pagination with multiple loops causes it to break
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- How to create a custom loop ordered by Categories on a Page Template?
- How can I implement pagination on custom category page?
- Order Categories by Character Count
- Remove “?category=” in the URL wordpress ajax filter work for category
- Replace category titles inside RSS feed