根据不同的class添加不同的样式:

我们希望给不同分类的文章加入 category-cat_name 。这篇里,我们就来介绍一下具体的方法

切换到主题目录,打开functions.PHP文件,加入以下代码

Crayon-5c891ce967a4e383395598" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891ce967a4e383395598-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce967a4e383395598-2">2
Crayon-num" data-line="Crayon-5c891ce967a4e383395598-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce967a4e383395598-4">4
Crayon-num" data-line="Crayon-5c891ce967a4e383395598-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce967a4e383395598-6">6
Crayon-num" data-line="Crayon-5c891ce967a4e383395598-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce967a4e383395598-8">8
Crayon-num" data-line="Crayon-5c891ce967a4e383395598-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce967a4e383395598-10">10
Crayon-num" data-line="Crayon-5c891ce967a4e383395598-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891ce967a4e383395598-12">12
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891ce967a4e383395598-1">Crayon-e">add_filterCrayon-sy">(Crayon-s">'body_class'Crayon-sy">,Crayon-s">'add_category_to_single'Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891ce967a4e383395598-2">Crayon-t">functionCrayon-h"> Crayon-e">add_category_to_singleCrayon-sy">(Crayon-sy">$Crayon-v">classesCrayon-sy">,Crayon-h"> Crayon-sy">$Crayon-t">classCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891ce967a4e383395598-3">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-e">is_singleCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891ce967a4e383395598-4">Crayon-h"> Crayon-m">globalCrayon-h"> Crayon-sy">$Crayon-v">postCrayon-sy">;
Crayon-line" id="Crayon-5c891ce967a4e383395598-5">Crayon-h"> Crayon-st">foreachCrayon-sy">(Crayon-sy">(Crayon-e">get_the_categoryCrayon-sy">(Crayon-sy">$Crayon-v">postCrayon-v">IDCrayon-sy">)Crayon-sy">)Crayon-h"> Crayon-st">asCrayon-h"> Crayon-sy">$Crayon-v">categoryCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891ce967a4e383395598-6">Crayon-h"> Crayon-c">// add category slug to the $classes array
Crayon-line" id="Crayon-5c891ce967a4e383395598-7">Crayon-h"> Crayon-sy">$Crayon-v">classesCrayon-sy">[Crayon-sy">]Crayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'category-'Crayon-sy">.Crayon-sy">$Crayon-v">categoryCrayon-v">category_nicenameCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891ce967a4e383395598-8">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891ce967a4e383395598-9">Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891ce967a4e383395598-10">Crayon-h"> Crayon-c">// return the $classes array
Crayon-line" id="Crayon-5c891ce967a4e383395598-11">Crayon-h"> Crayon-st">returnCrayon-h"> Crayon-sy">$Crayon-v">classesCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891ce967a4e383395598-12">Crayon-sy">}

修改以后,输出如下:

Crayon-5c891ce967a4f486355882" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891ce967a4f486355882-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891ce967a4f486355882-1">Crayon-e">single Crayon-v">singleCrayon-o">-Crayon-e">post Crayon-v">postidCrayon-o">-Crayon-cn">90Crayon-h"> Crayon-v">singleCrayon-o">-Crayon-v">formatCrayon-o">-Crayon-e">standard Crayon-v">loggedCrayon-o">-Crayon-st">inCrayon-h"> Crayon-v">adminCrayon-o">-Crayon-e">bar Crayon-v">categoryCrayon-o">-Crayon-e">faqs Crayon-v">customizeCrayon-o">-Crayon-v">support

这是一篇FAQS分类下的文章,所以添加了 category-faqs 这个class,其他分类文章中会输出其他分类的class。

感兴趣的朋友可以测试一下。

原文链接:/wordpress/238410.html

猜你在找的wordpress相关文章