Show/hide css based on categories

This topic contains 11 replies, has 4 voices, and was last updated by  Paolo 7 years, 4 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #322956

    James Veale
    Expired Member
    Post count: 54

    I want to hide certain elements on individual categories…..I’ll explain…

    Example:

    If the category is ‘Rivers’ hide css/info* related to ‘lake size’.

    If the category is ‘Lakes’ hide css/info* relates to ‘rivers’

    I know I could split rivers and lakes into 2 custom post types, but I’d lose a bunch of other things like having everything neatly on the same map etc..

    *custom field info

    #323116

    Guust
    Moderator
    Post count: 29970

    I don’t think that is possible. Let’s ask the others too.

    #323385

    James Veale
    Expired Member
    Post count: 54

    I suppose I’m basing it on this sort of idea with pages:

    
    
    <?php // Do action only on specific page in WP ?>
    <?php if( in_page('johns lake') ) { ?>
    <style>
      #river_info{
          display: none;
      }
    </style>
    <?php } ?>

    Not sure if it would be possible to do the same with categories though

    #323697

    Paolo
    Site Admin
    Post count: 31206

    Hi,

    each page and category archive should have a unique body class.

    You can use that to target your css only for specific pages.

    Example: that “johns lake” page, probably has a body class like this: page-id-123 (where 123 is the page ID)

    In that case you can use css like this:

    
    
    
    .page-id-123  #river_info{
          display: none;
      }

    Hope this helps,

    Let us know

    #323826

    James Veale
    Expired Member
    Post count: 54

    Useful to know, so I could potentially edit out info on a case by case basis when new pages get added…

    But there’s no way to do it for an entire category by the sound of it.

    Can you guys think of another way to keep everything as one cpt but display only specific fields (or hide specific fields if it’s a different category)?

    #323840

    Paolo
    Site Admin
    Post count: 31206

    Hi,

    what do you mean by: “for a category”?

    Archives add a custom body class to for each category.

    If you provide a link of an example, I’ll point you in the right direction.

    Thanks

    #323848

    James Veale
    Expired Member
    Post count: 54

    On the map for example

    I have “Lakes” and “rivers” … Its great having both on the same map… (having individual CPTs would also be an easy way to separate data, but sometimes there are rivers and lakes on the same fishing venue). It doesn’t make sense having the question “What is the lake size?” on the add listing for rivers… but I can live with that…

    What really doesn’t make sense is looking at a river such as this and clicking the ‘more info’ tab and seeing stuff about lake size… its a river, plus… pretty difficult to know the size anyway.. so it would be better just to hide that info for rivers. Equally there is info about rivers that we still have to add, but that info doesn’t apply to lakes.

    Sorry if that’s super confusing! It does sound it as I’m typing it!

    #323864

    Paolo
    Site Admin
    Post count: 31206

    In this case you linked the home page and a detail page.

    Neither one is a category page like you mentioned.

    For something like that the only option is using separate CPT.

    Thanks

    #323865

    Guust
    Moderator
    Post count: 29970

    Another suggestion is to create price packages for lakes and rivers separate, and then restrict some custom fields to the relevant categories. That way they cannot be added for that category and they will not show for that category.

    #323880

    James Veale
    Expired Member
    Post count: 54

    Genius! I think that’s it!

    #329190

    Anitra Larsen
    Full Member
    Post count: 52

    I’m not an advanced programmer, but pieced together information from a couple different support topics and added this my child theme’s functions file to get a class for one specific GeoDirectory Place Category. Now I can style that category as I want.

    
    
    function my_custom_body_class_in_category($classes) {
        // add 'my-class' to the my-category archive and single posts of the category. 
        if ( has_term ( 'my_class', 'gd_placecategory' ) )
            $classes[] = 'my_class';
        // return the modified $classes array
        return $classes;
    }
     
    // add my custom class via body_class filter
    add_filter('body_class','my_custom_body_class_in_category');
    #329239

    Paolo
    Site Admin
    Post count: 31206

    Way to go Anitra and thanks for sharing! 🙂

Viewing 12 posts - 1 through 12 (of 12 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket