Php 如果页面具有自定义分类法,则获取\u模板\u部分
我试图显示一个不同的标题菜单,这取决于页面或帖子被标记的类别。我创建了两个工具集分类法:类别和酒店 我希望所有酒店都有自己的菜单、徽标和预订菜单,但我无法让php工作并针对类别 我尝试了几个片段,例如这一个:Php 如果页面具有自定义分类法,则获取\u模板\u部分,php,wordpress,get,advanced-custom-fields,Php,Wordpress,Get,Advanced Custom Fields,我试图显示一个不同的标题菜单,这取决于页面或帖子被标记的类别。我创建了两个工具集分类法:类别和酒店 我希望所有酒店都有自己的菜单、徽标和预订菜单,但我无法让php工作并针对类别 我尝试了几个片段,例如这一个: *< ?php if( has_term('skaga', 'hoteller') { ** 我还尝试了另一个在不同站点上工作的分类法,但它针对两种分类法,我只需要它针对一种分类法 *< ?php if( has_term('puder', 'produkttype') ||
*< ?php if( has_term('skaga', 'hoteller') {
*<?php if(有术语('skaga','hoteller')){
获取模板部分('includes/partials/header/header menu skaga');
}?>*
我还尝试了另一个在不同站点上工作的分类法,但它针对两种分类法,我只需要它针对一种分类法
*< ?php if( has_term('puder', 'produkttype') || has_term('senge', 'produkttype')) {
php if(has_term('puder','produkttype')| | has_term('senge','produkttype')){
获取模板部分('partials/sections/section','trustpilot');}>*
我希望有人能指出我做错了什么。非常感谢所有的帮助
谢谢你我不确定为什么WP core
有_term()
函数在这种情况下不起作用,但是你可以做如下操作,使用获取_terms()
并对结果执行数组搜索()
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' );
}
我不知道为什么WP core的函数在这种情况下不起作用,但是您可以执行如下操作,使用获取\u terms()
并对结果执行数组搜索()
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' );
}
@本吉
嗨,本
谢谢你的回复
我已经试过了,试过了,在其他分类法中也试过了,所以他们得到了另一个模板部分,但运气不好
这就是我试图使用它的方式你能发现错误的部分吗
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' );
}
再次感谢!@Benj
嗨,本
谢谢你的回复
我已经试过了,试过了,在其他分类法中也试过了,所以他们得到了另一个模板部分,但运气不好
这就是我试图使用它的方式你能发现错误的部分吗
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' );
}
再次感谢!您的代码是否在have_posts()中
loop?您可能需要添加$postId
值,否则它就完全如上图所示。我不是php高手,所以我不知道如何准确地使用$postId编写它。我似乎无法让四个if语句中的两个正常工作。不是skaga和sabro。它正常工作,我只是没有检查最后两个分类法的两个首页。这样一个upid错误,但至少它起作用了。谢谢你的帮助Ben!你的代码在have_posts()中吗
loop?您可能需要添加$postId
值,否则它就完全如上图所示。我不是php高手,所以我不知道如何准确地使用$postId编写它。我似乎无法让四个if语句中的两个正常工作。不是skaga和sabro。它正常工作,我只是没有检查最后两个分类法的两个首页。这样一个我错了,但至少它起作用了。谢谢你的帮助,本!