how to CSS select all posts in a category (7 posts)

  1. nfdesign
    Posted 2 years ago #

    It seems very straight forward in pages to CSS select all the child pages of a parent.
    such as: .page-child.parent-pageid-119

    I cannot seem to figure out how to select all the posts within a category, is this possible?


  2. wpfan1000
    Posted 2 years ago #

    Have you tried using Firebug addon in Firefox browser?

  3. nfdesign
    Posted 2 years ago #

    I have been using Inspect Element in Chrome, but tried Firebug as well.

    It is not under body class like it is for the pages

  4. wpfan1000
    Posted 2 years ago #

    Well eg for posts in the category "safety" I have

    <body class="archive category category-safety category-10 logged-in admin-bar custom-background custom-font-enabled single-author customize-support">

    So there is the class "category-safety" created by WordPress in the body tag.

    Would that help?

  5. nfdesign
    Posted 2 years ago #

    no you are talking about when you are looking at the category page, I am talking about if you click on one of the titles on the category page, that takes you to the post page.

    What I get there is:
    <body class="single single-post postid-300 single-format-standard logged-in admin-bar full-width-content customize-support">

    I want to be able to tell any single posts in that category to have the same CSS style no matter how many new posts are added in the category they will all have the style.

    I did achieve this with pages and parent pages, but I am not sure how to do this with the posts and categories.

  6. wpfan1000
    Posted 2 years ago #

    My individual posts have the category in the article tag:

    <article class="post-3901 post type-post status-publish format-standard hentry category-safety" id="post-3901">

    eg you should be able to use "category-safety" to style each post that is in that category

  7. Michael
    Forum Moderator
    Posted 2 years ago #

    the body_class() does not output category classes in single posts; you might need to add a filter;


    have a close look at the second example...
    possibly adapt it this way:

    // add category nicenames in body_class
    function category_id_class($classes) {
    	global $post;
    	if( is_single() ) {
    	foreach((get_the_category($post->ID)) as $category) $classes[] = 'category-' . $category->category_nicename;
    	return $classes;
    add_filter('body_class', 'category_id_class');

Topic Closed

This topic has been closed to new replies.

About this Topic