根据分配的标签将自定义 class 添加到 <body>
Add custom class to <body> based on assigned tag
WordPress 内容管理系统。我有一个自定义分类法 - 'csgroup' 和它的术语 - 'expertise-page'。当我将术语 'expertise-page' 分配给页面时,我需要将自定义 class 'new-class' 添加到 <body>
。
这是 function.php 文件中的函数,但这不起作用:
function custom_body_classes( $classes ){
if ( is_tag('csgroup', 'expertise-page') ) {
$classes[] = 'new-class';
}
return $classes;
}
add_filter( 'body_class', 'custom_body_classes' );
is_tag();功能将仅在标签存档页面上 运行 而不是在每个页面上 post 或自定义 post 类型;我猜你正在寻找。
检查分类法(例如 csgroup)中的任何术语(例如 expertise-page)是否分配给当前 post、页面或自定义 post 类型;您将不得不使用 WordPress 的 has_term() 功能。
此函数确认当前页面 post 或自定义 post 类型是否附加了您想要的分类术语。
您可以在 WordPress 开发人员文档中了解有关此功能的更多信息以及更多信息。这是它的 link; has_term()
希望这能解释并解决您的问题。
function custom_body_classes( $classes ){
if ( has_term('expertise-page', 'csgroup') ) {
$classes[] = 'new-class';
}
return $classes;
}
add_filter( 'body_class', 'custom_body_classes' );
WordPress 内容管理系统。我有一个自定义分类法 - 'csgroup' 和它的术语 - 'expertise-page'。当我将术语 'expertise-page' 分配给页面时,我需要将自定义 class 'new-class' 添加到 <body>
。
这是 function.php 文件中的函数,但这不起作用:
function custom_body_classes( $classes ){
if ( is_tag('csgroup', 'expertise-page') ) {
$classes[] = 'new-class';
}
return $classes;
}
add_filter( 'body_class', 'custom_body_classes' );
is_tag();功能将仅在标签存档页面上 运行 而不是在每个页面上 post 或自定义 post 类型;我猜你正在寻找。
检查分类法(例如 csgroup)中的任何术语(例如 expertise-page)是否分配给当前 post、页面或自定义 post 类型;您将不得不使用 WordPress 的 has_term() 功能。
此函数确认当前页面 post 或自定义 post 类型是否附加了您想要的分类术语。
您可以在 WordPress 开发人员文档中了解有关此功能的更多信息以及更多信息。这是它的 link; has_term()
希望这能解释并解决您的问题。
function custom_body_classes( $classes ){
if ( has_term('expertise-page', 'csgroup') ) {
$classes[] = 'new-class';
}
return $classes;
}
add_filter( 'body_class', 'custom_body_classes' );