For the example you posted, the article is styled according to the css rule(s) for category-selling-and-advertising
which is defined last (among the category-*
classes) in the article’s class
attribute.
You need to intercept the functionality of post_class()
to change the order these classes appear. The post_class
filter will do the job for you:
add_filter(
'post_class',
function($classes) // $classes is an array
{
$cats=get_the_category();
$master_cat=$cats[0]->slug;
if($k=array_search("category-$master_cat",$classes))
{
unset($classes[$k]);
$classes[]="category-$master_cat";
}
return $classes;
}
);
The above is a rather naive solution, but I think it’s a good place to start.
Related Posts:
- Showing current subcategory
- Show all child categories associated to post ID within loop
- Get the children of the parent category
- How can I custom order the results from wp_list_categories?
- wp_list_categories: get latest featured_image of category
- Exclude category by slug for for each loop
- Display page of custom posts?
- Why is my loop not dynamically grabbing the correct Category and displaying all categorized posts?
- Query Posts Exclude Entire Category
- Publish Categories for a particular Author on a given Post
- Showing categories and subcategories with posts
- Loop Problem: Displays same set of post for all categories
- Category ID returns as ‘0’?
- Three Column Loop
- Exclude category from get_the_category
- Display all categories as plain text
- loop inside a loop : search for posts in the same category
- Secondary loop cuts off at 10 posts?
- Need a conditional to test if title of parent page matches title of child page
- My custom query not detecting the correct category
- Show last post from multiple categories using wp_list_categories
- How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
- How to get Author ID outside the loop
- remove tags from the_content
- Why do themes rely on “The Loop”?
- How to place comments_template(); outside the loop?
- Endless loop with wp_insert_post and wp_update_post
- Hook into the loop via a plugin, and output something after every X post?
- Two custom loops, pagination, offset
- Access to Media Library
- Display one latest post from multiple categories
- Getting two wp_link_pages output
- post loop causes wp_users and wp_usermeta DB queries for each users
- How do I get the total number of categories in a list of search results?
- Run The Loop over array of post objects
- Enable infinite scroll on single.php [closed]
- How to exclude specific category from the get_the_category(); array
- a-z list, categories and sub categories in loop
- How to retrieve “sticky” post outside the “loop”?
- Show all posts for a specific category/author
- code suddenly appearing from my tempate second loop
- Get post/page data outside the loop
- create a page which displays a list of categories title+ short description?
- woocommerce get_price_html not pulling in correct price
- Trouble Formatting DateTime
- Order archive results by post id in custom taxonomy template
- How to fix pagination for custom loops?
- pre_get_posts for two loops on same page
- Modify the main loop to display current month / year
- Problem with displaying HTML content after in_array [closed]
- Loop for sticky posts
- Get author_id in wordpress
- Single.php different behaviour from admin to non-admin
- Array ids post to function have_post
- Can’t access PHP array inside script localization from javascript
- ACF – add a group inside a repeater [closed]
- Give each posts in a loop a number in sequence
- Get post offset/posts page offset in single post page (outside the loop)
- get_comments with post_status ‘public’ retrieves NULL result
- Do not show sub categories in the loop on archive-product.php
- Content Visibility for WordPress is not working (Drafts, public etc.)
- Generating a perfect loop
- I have broke my pagination, same posts on all pages (index.php)
- Filter existing hook using parameter and foreach loop
- Run “the loop” in a random order
- WordPress loop not working only on home page, works on all other pages
- Show multiple categories in query using redux framework variable
- Search page loop banner image issue
- Video post format, extra content breaks video embed
- Why is my category post repeating twice
- Get post category as a separate string and url
- Adding a featured image to a foreach WordPress loop
- Getting selection of posts based on loop variables via Ajax
- Why would this code work locally but break online?
- Why would a completely normal loop give offset errors and maximum execution time errors?
- Avoid WP_Query’s duplicate posts with taxonomies
- How can I change how my tag pages display?
- Properly loop through and paginate page on each function call
- Get a Post Loop based on Logged in User information into a Shortcode
- How to show related post with category DESC (ASC) IDs of ID current post
- Submit form for loop of products of the term with AJAX
- Last post in loop when even
- Customizer settings using “for” loop
- Querying posts from two different categories while looping inside another loop
- Loop doesn’t work
- Repeat code, change variable?
- Foreach loop inside foreach loop?
- Need list of child category post titles sorted by child category
- Ajax category pagination problem
- Unable to login + Redirect loop on mobile
- Category slug in in loop always the same?
- Variable not working in sidebar loop
- Combine Query and Select Database to create directory loop
- 2 loops in blog homepage
- wordpress showing all posts instead of date range
- Help with add_filter(‘the_content’, ‘some_function’) and multiple matches
- Single Loop With Dual Content Area
- How to do query_posts on tags pages
- Showing ads after posts in home page
- Use Base-URL with Query-Loop