Php 从函数中排除某些分类法
我需要一个WordPress代码的帮助。 在这个页面上,我得到了一个返回分类法及其术语的代码 但是我想从这个列表中排除一种叫做“广告类型”的分类法 我用这个代码设法从“广告类型”中排除了一些术语Php 从函数中排除某些分类法,php,wordpress,Php,Wordpress,我需要一个WordPress代码的帮助。 在这个页面上,我得到了一个返回分类法及其术语的代码 但是我想从这个列表中排除一种叫做“广告类型”的分类法 我用这个代码设法从“广告类型”中排除了一些术语 // An array of IDs to ignore / exclude $ excluded_ids = array (1, 2, 3, 4); foreach ($ terms as $ term) { // Only proceed if the term_id is NOT in the
// An array of IDs to ignore / exclude
$ excluded_ids = array (1, 2, 3, 4);
foreach ($ terms as $ term) {
// Only proceed if the term_id is NOT in the $ excluded_ids array
if (! in_array ($ term-> term_id, $ excluded_ids)) {
$ out. = '<li> <a href="' .get_term_link($term-> slug, $ taxonomy).' '>'. $ term-> name. '</a> </ li>';
}
}
//要忽略/排除的ID数组
$excluded_id=数组(1,2,3,4);
foreach($terms作为$term){
//仅当术语_id不在$excluded_id数组中时才继续
if(!in_数组($term->term_id,$excluded_id)){
$out.='';
}
}
但我想要的是“广告类型”分类法根本不出现。
因为在上面的代码中,“广告类型”的标题仍然出现在检索到的术语列表中。
我想要的是完全避免在wordpress模板的那个部分检索到这种分类法
代码如下:
function wpdocs_custom_taxonomies_terms_links() {
// Get post by post ID.
$post = get_post( $post->ID );
// Get post type by post.
$post_type = $post->post_type;
// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}
函数wpdocs\u自定义\u分类法\u术语\u链接(){
//通过邮政ID获取邮政信息。
$post=get\u post($post->ID);
//通过post获取post类型。
$post\U type=$post->post\U type;
//获取post类型分类。
$taxonomies=get_object_taxonomies($post_type,'objects');
$out=array();
foreach($taxonomy作为$taxonomy\u slug=>$taxonomy的分类法){
//获取与post相关的术语。
$terms=获取术语($post->ID,$taxonomy\u slug);
如果(!空($terms)){
$out[]=''.$taxonomy->label.:\n“;
foreach($terms作为$term){
$out[]=sprintf('- %2$s
-',
esc_url(获取_term_链接($term->slug,$taxonomy_slug)),
esc_html($term->name)
);
}
$out[]=“
\n”;
}
}
返回内爆(“”,$out);
}
下面是我如何在模板页面上检索它:
<?php echo wpdocs_custom_taxonomies_terms_links(); ?>
有办法做到这一点吗?
有人能帮我吗
谢谢我不知道我的比较是否正确,但是你可以检查当前的分类法是否不是
广告类型
,然后才能获取术语
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy_slug !== 'ad_type') :
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
endif;
}
foreach($taxonomy作为$taxonomy\u slug=>$taxonomy的分类法){
如果($taxonomy\u slug!=='ad\u type'):
//获取与post相关的术语。
$terms=获取术语($post->ID,$taxonomy\u slug);
如果(!空($terms)){
$out[]=''.$taxonomy->label.:\n“;
foreach($terms作为$term){
$out[]=sprintf('- %2$s
-',
esc_url(获取_term_链接($term->slug,$taxonomy_slug)),
esc_html($term->name)
);
}
$out[]=“
\n”;
}
endif;
}
我不知道我的比较是否正确,但您可以检查当前分类法是否不是广告类型
,然后获取术语
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy_slug !== 'ad_type') :
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
endif;
}
foreach($taxonomy作为$taxonomy\u slug=>$taxonomy的分类法){
如果($taxonomy\u slug!=='ad\u type'):
//获取与post相关的术语。
$terms=获取术语($post->ID,$taxonomy\u slug);
如果(!空($terms)){
$out[]=''.$taxonomy->label.:\n“;
foreach($terms作为$term){
$out[]=sprintf('- %2$s
-',
esc_url(获取_term_链接($term->slug,$taxonomy_slug)),
esc_html($term->name)
);
}
$out[]=“
\n”;
}
endif;
}
这个问题有点令人困惑,因为您说“完全排除返回的分类法”,并且您有两个独立的代码块。因此-函数get_the_terms()
不允许排除术语类型,除非您要对其使用筛选器(但该筛选器将在每次调用get_the_terms时运行,包括任何管理员端调用)。在函数内部发生“排除”是可以接受的吗?这个问题有点令人困惑,因为您说“完全排除返回的分类法”,并且您有两个独立的代码块。因此-函数get_the_terms()
不允许排除术语类型,除非您要对其使用筛选器(但该筛选器将在每次调用get_the_terms时运行,包括任何管理员端调用)。在您的函数中发生“排除”是可以接受的吗?