Site logo

Archived topic

Show/Hide element per category

7 replies · Started by Antonio on October 21, 2015

Viewing posts 1–8 of 8

Hi
is it possible to show/hide post elements per category?
For example, into News category I want to show the date and the category, not the author. In Products category instead I want to not show the author neither the date but the category yes.

Hi Antonio. Are you wanting to hide the post elements on the category archive page, or on the single post page?

On both, if possibile.

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.

Hi there,

I would also like to hide the author and date on single posts or post categories.

I tried the code above and got the following error:

Warning: Missing argument 2 for add_category_to_single() in /home/eersdiek/public_html/wp-content/themes/generatepress_child/functions.php on line 10
class="single single-post postid-1921 single-format-aside logged-in admin-bar no-customize-support fl-builder post-image-below-header post-image-aligned-center secondary-nav-above-header secondary-nav-aligned-right sections-no-sidebars sticky-menu-no-transition navigation-logo sticky-enabled menu-logo menu-logo-enabled has-dashicons leiding-elke-dag generatepress full-width-content no-sidebar nav-below-header fluid-header separate-containers active-footer-widgets-0 nav-aligned-left header-aligned-center dropdown-hover">

Is there an easier way of doing this?

Regards,
Jan

Hi - is there a way to unhide an entry element (like post date) that was previously CSS display:none?

Hi there,

this is a really old topic - could you start a new topic where you can privately share a link to your site and we can provide some help.

This archived topic is closed to new replies.