Add the corresponding class to the page according to the WordPress classification ID

WordPress twenty 2.9K Reading mode

This code automatically adds the<body>tag on the category page and article page by judging the WordPress category ID“ cat-6-id ”For example:

< body class =" archive category category-wordpress category-6 logged-in custom-background cat-6-id ">

The number is the ID number of the category, provided that the theme template must use the function:

  1. <body <? php body_class();  ?>>

With this very convenient function, you can define different styles for different classifications.

 Add the corresponding class to the page according to the WordPress classification ID

WordPress

Add the following code to the functions.php file of your current theme:

  1. function  category_id_class( $classes ) {
  2.       global   $post ;
  3.            foreach ((get_the_category( $post ->ID))  as   $category )
  4.                 $classes  [] = 'cat-' .   $category ->cat_ID .  '-id';
  5.       return   $classes ;
  6. }
  7. add_filter('post_class', 'category_id_class');
  8. add_filter('body_class', 'category_id_class');

 

Application examples:

For example: HotNewspro Theme , hide the hot articles (the top four pictures) on the classified archive and article pages with the classification ID of 6, and add the following in the theme style file style:

  1. .cat-6-id,  #top _hot {
  2.      display none ;
  3.     }

It can be used to define the text size and background color at any other location different from other classifications.

Source of original code: http://www.trickspanda.com/2014/04/add-category-id-body-post-class-wordpress/

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments twenty    Visitors eighteen    Author one
    •  Cixian Encyclopedia
      Cixian Encyclopedia one

      I don't quite get it, but I think you are really terrific!

      •  Superior reunion
        Superior reunion one

        Very good, learning, support.

        •  kpopcomedy
          kpopcomedy six

          Brother Bird, where is your class=logo defined?, Want to make the logo class smaller

            •  Robin
              Robin

              @ kpopcomedy Open the theme style file:
              .logo {    background:url(images/logo.png) no-repeat;    float: left;    width: 350px;    height: 53px;    margin: 5px 0 0 5px;    text-indent: –10000px;    cursor:pointer;    }
              Modify:
              width: 350px;
              height: 53px;

            •  wmtimes
              wmtimes seven

              It seems that the article can also come like this.

              •  Moisten
                Moisten five

                Brother Bird. There are more than 20 friendship links on the home page. Add other links. The home page is not displayed. What shall I do?

                •  kpopcomedy
                  kpopcomedy six

                  Brother Bird, how can I remove the words below the navigation bar? After I deleted the line of code corresponding to the display time in head.php, there was only "login" left, but the code did not directly display login, and then I didn't know what to do.

                  •  Trees on the tuyere
                    Trees on the tuyere three

                    Brother Bird's homepage has changed a lot recently

                    •  kpopcomedy
                      kpopcomedy six

                      The microblog has been removed and the class (logo) has been reduced, but there is still a blank area next to the logo image as the homepage link

                      •  hjrhclan
                        hjrhclan zero

                        What if an article has many categories?

                        •  Xiao Linzi
                          Xiao Linzi one

                          Brother Bird, how to ask Wordpress home page not to display articles on the specified home page? The latest article list still appears

                          •  Long Xiaotian
                            Long Xiaotian five

                            Hello, where does the classification ID number 6 add to this function?

                              •  Long Xiaotian
                                Long Xiaotian five

                                @ Long Xiaotian Oh, I see. This function means adding cat id number id to the body of all categories and classified articles

                              •  Hao
                                Hao one

                                I want to ask you, when I build a classification directory, how can I do if the ID is not 2? The newly created category directory 1345678 has no category directory ID 2. Why? thank you

                               anonymous

                              Comment

                              Anonymous netizens
                               :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                              Drag the slider to complete validation