get_template_part 如果页面有自定义分类
get_template_part if page has a custom taxonomy
我试图根据页面或 post 被标记的类别显示不同的 header 菜单。我创建了两个工具集分类法、类别和酒店。
我希望所有酒店都有自己的菜单、徽标和预订菜单,但我无法让 php 工作并定位类别。
我试过几个片段,例如这个:
*< ?php if( has_term('skaga', 'hoteller') {
get_template_part('includes/partials/header/header-menu-skaga');
}?>*
我也试过另一个在不同网站上工作的,但它针对两个分类,我只需要它针对一个。
*< ?php if( has_term('puder', 'produkttype') || has_term('senge', 'produkttype')) {
get_template_part('partials/sections/section','trustpilot'); } ?>*
我希望有人能指出我做错了什么。非常感谢所有帮助!
谢谢
我不确定为什么 WP 核心 has_term()
函数在这种情况下不起作用,但您可以执行以下操作,使用 get_the_terms()
并执行 array_search()
在结果上。
function has_custom_taxonomy_term($termSlug, $taxonomy, $postId = null) {
$terms = get_the_terms($postId, $taxonomy);
if (isset($terms->errors)) {
// taxonomy doesn't exist
return false;
}
return array_search($termSlug, array_column($terms, 'slug')) !== false;
}
if (has_custom_taxonomy_term('skaga', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-skaga' );
}
@Benj
嗨本,
感谢您的回复。
我已经试过了,并尝试对其余的分类法使用 and 和 elseif,所以他们得到了 get_ 另一个模板部分,但没有成功。
这是我尝试使用它的方式,你能找出错误的部分吗?
function has_custom_taxonomy_term($termSlug, $taxonomy, $postId = null) {
$terms = get_the_terms($postId, $taxonomy);
if (isset($terms->errors)) {
// taxonomy doesn't exist
return false;
}
return array_search($termSlug, array_column($terms, 'slug')) !== false;
}
if (has_custom_taxonomy_term('sabrokro', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-sabrokro' );
}
elseif (has_custom_taxonomy_term('odder-parkhotel', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-odder' );
}
elseif (has_custom_taxonomy_term('skaga', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-skaga' );
}
elseif (has_custom_taxonomy_term('hotel-hanstholm', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-hanstholm' );
}
再次感谢!
我试图根据页面或 post 被标记的类别显示不同的 header 菜单。我创建了两个工具集分类法、类别和酒店。
我希望所有酒店都有自己的菜单、徽标和预订菜单,但我无法让 php 工作并定位类别。
我试过几个片段,例如这个:
*< ?php if( has_term('skaga', 'hoteller') {
get_template_part('includes/partials/header/header-menu-skaga'); }?>*
我也试过另一个在不同网站上工作的,但它针对两个分类,我只需要它针对一个。
*< ?php if( has_term('puder', 'produkttype') || has_term('senge', 'produkttype')) {
get_template_part('partials/sections/section','trustpilot'); } ?>*
我希望有人能指出我做错了什么。非常感谢所有帮助!
谢谢
我不确定为什么 WP 核心 has_term()
函数在这种情况下不起作用,但您可以执行以下操作,使用 get_the_terms()
并执行 array_search()
在结果上。
function has_custom_taxonomy_term($termSlug, $taxonomy, $postId = null) {
$terms = get_the_terms($postId, $taxonomy);
if (isset($terms->errors)) {
// taxonomy doesn't exist
return false;
}
return array_search($termSlug, array_column($terms, 'slug')) !== false;
}
if (has_custom_taxonomy_term('skaga', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-skaga' );
}
@Benj
嗨本,
感谢您的回复。
我已经试过了,并尝试对其余的分类法使用 and 和 elseif,所以他们得到了 get_ 另一个模板部分,但没有成功。
这是我尝试使用它的方式,你能找出错误的部分吗?
function has_custom_taxonomy_term($termSlug, $taxonomy, $postId = null) {
$terms = get_the_terms($postId, $taxonomy);
if (isset($terms->errors)) {
// taxonomy doesn't exist
return false;
}
return array_search($termSlug, array_column($terms, 'slug')) !== false;
}
if (has_custom_taxonomy_term('sabrokro', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-sabrokro' );
}
elseif (has_custom_taxonomy_term('odder-parkhotel', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-odder' );
}
elseif (has_custom_taxonomy_term('skaga', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-skaga' );
}
elseif (has_custom_taxonomy_term('hotel-hanstholm', 'hoteller')) {
get_template_part( 'includes/partials/header/header-menu-hanstholm' );
}
再次感谢!