从功能中排除某些分类

时间:2017-11-12 15:26:55

标签: php wordpress

我需要WordPress代码的帮助。 在这个页面上https://developer.wordpress.org/reference/functions/get_the_terms/我得到了一个返回我的分类法及其术语的代码。

但我喜欢某种名为" Ad Type"被排除在此列表之外。

我设法将某些字词排除在"广告类型"使用此代码...

// 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>';
}
}

但我想要的是&#34;广告类型&#34;分类法根本没有出现。 因为上面的代码是&#34;广告类型&#34;仍然出现在检索到的术语列表中。 我想要的是完全放弃在我的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 ); 
}

以下是我在模板页面上检索它的方法:

<?php echo wpdocs_custom_taxonomies_terms_links(); ?>

有办法做到这一点吗? 有人可以帮助我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我不知道我是否正确比较,但您可以检查当前的分类法是否Ad Type只有获得条款

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;
}