Cyber Week Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] Show/Hide element per category

Home Forums Support Show/Hide element per category

  • This topic has 7 replies, 5 voices, and was last updated 7 months ago by David.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #146258
    Antonio

    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.

    #146325
    bdbrown

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

    #146412
    Antonio

    On both, if possibile.

    #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.

    #228642
    Jan

    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

    #228651
    Jan

    Hi there,

    Please Ignore above or delete it. I found another way in:

    https://generatepress.com/forums/topic/move-author-date-to-the-bottom-after-content/

    Jan

    #1267105
    Fazz

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

    #1267329
    David
    Staff
    Customer Support

    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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.