WordPress.org

Forums

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

  1. nfdesign
    Member
    Posted 1 year 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?

    thanks

  2. wpfan1000
    Member
    Posted 1 year ago #

    Have you tried using Firebug addon in Firefox browser?

  3. nfdesign
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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. the body_class() does not output category classes in single posts; you might need to add a filter;

    http://codex.wordpress.org/Function_Reference/body_class
    http://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters

    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

Tags