"how to" - use post categories as body tags for category-selective CSS-formating

resolved (2 posts) (1 voice)

  1. HeliR
    Member

    Problem: several parts of the website should hide/display META-Information differently

    Default: in Admin-Panel the Meta-Information can bei either shown or hidden, but not category-specific

    put following function into themes / lib / functions / contex.php or in your own function.php

    function HeliListCatString($PostID) {
    $post_categories = wp_get_post_categories( $PostID );
    foreach($post_categories as $c){
    $cat = get_category( $c );
    $r = $r . ' ' . $cat->slug;
    }
    return trim($r);
    }

    now you can put in the same file contex.php following bold line in the function mysite_body_class - within the part starting with is_singular

    function mysite_body_class( $class = array() ) {
    .....
    # Is singluar post
    if( is_singular() ) {
    .....
    $classes[] = $post->ID;

    $classes[] = HeliListCatString($post->ID);

    if( $type == 'portfolio' )
    ......

    having done that you get for your post new body-class CSS tags that refer to the category-slugs. Now you can specific put meta on/off depending on the body-class by adding into your style.css

    having post-meta in the admin-panel ON I do now following:

    here I switch showing post-meta off for entire site but put it ON in the sites part categorized with slug RESEARCH

    /*CSS Declarations: turn META OFF in entire site / turn META ON in RESEARCH Branche */
    * p.post_meta {display:none;}
    body.RESEARCH p.post_meta {display:block;}

    Posted 7 months ago #
  2. HeliR
    Member

    actually, after programming that part I discovered that there is already a CATEGORY specific tag in the post (NEW in 1.4 - I have looked for that in 1.3 and didnt see it?)

    I found that in the <div id="main_inner"> part of the post.
    BUT: this does not allow to format everything outside that DIV (e.g. Header) in a category - specific way - so the solution I suggested above may make sense for one or the other reader.

    /* CSS Code to hide bottom META in entire site - only show in the RESEARCH-Branch of the site */
    * p.post_meta_bottom {display:none;}
    body.RESEARCH p.post_meta_bottom {display:block;}

    Best regards, Heli

    Posted 7 months ago #

Reply

You must log in to post.

Construct WordPress Theme
Construct wordpress theme
Myriad WordPress Theme
Myriad wordpress theme
Method WordPress Theme
Method wordpress theme
Fusion WordPress Theme
Fusion wordpress theme
Elegance WordPress Theme
Elegance wordpress theme
Echelon WordPress Theme
Echelon wordpress theme
Dejavu WordPress Theme
Dejavu wordpress theme
Modular WordPress Theme
Modular wordpress theme