Reply To: Show/Hide element per category

Home Forums Support Show/Hide element per category Reply To: Show/Hide element per category

Home Forums Support Show/Hide element per category Reply To: Show/Hide element per category

#146595
bdbrown

For single posts we need to add the category name to the page. Add this function to a child theme functions.php file:

/* add post category class to body tag */
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes, $class) {
    if (is_single() ) {
        global $post;
        foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
           $classes[] = $category->category_nicename;
        }
    }
    // return the $classes array
    return $classes;
}

Adding PHP Functions: https://generatepress.com/knowledgebase/adding-php-functions/

The individual post element classes are:
.entry-header .entry-meta = date and author
.entry-meta .posted-on = date
.entry-meta .byline = author
.entry-meta .cat-links = categories

Then add the appropriate CSS based on the category class.

For single posts in your News category it would be:

/* hide author on News single post */
.single .news .entry-meta .byline {
    display: none;
}

For your Products cateogry:

/* hide date and author on Products single post */
.single .products .entry-header .entry-meta {
    display: none;
}

Adding CSS: https://generatepress.com/knowledgebase/adding-css/

For the category archive page the category name is already in the <body> element in the form of “category-slugname” where slugname is the slug of the category. So you would use this:

/* hide post author on News archive page */
.archive .category-news .entry-meta .byline {
    display: none;
}

/* hide post date and author on Products archive page */
.archive .category-products .entry-header .entry-meta {
    display: none;
}

Let me know how that works.