web-dev-qa-db-ja.com

Get_the_tag_list()からタグを除外/フィルタリングする方法

Get_the_tag_list()によって生成されたHTML文字列からタグを除外/フィルタリングする方法を誰かが知っていますか?

http://codex.wordpress.org/Function_Reference/get_the_tag_list

任意の助けは大歓迎です。

2
Ben Pearson
function mytheme_filter_tags( $term_links ) {
    $result = array();
    $exclude_tags = array( 'some tag', 'another tag', 'third tag' );
    foreach ( $term_links as $link ) {
        foreach ( $exclude_tags as $tag ) {
            if ( stripos( $link, $tag ) !== false ) continue 2;
        }
        $result[] = $link;
    }
    return $result;
}
add_filter( "term_links-post_tag", 'mytheme_filter_tags', 100, 1 );

// do loop stuff
echo get_the_tag_list('<p>Tags: ',', ','</p>');
// end loop stuff

remove_filter( "term_links-post_tag", 'mytheme_filter_tags', 100 );
0
Eugene Manuilov

これは私のブログの1つからの抜粋です。

function get_filtered_tags($post_id) {
    // the slugs to be INCLUDED in the term list:
    $primary_tags = array( 'books', 'travel', ... ); 

    $post_tags = wp_get_object_terms($post_id, 'post_tag');

    if( ! empty( $post_tags ) ) {
        if( ! is_wp_error( $post_tags )) {
            $tag_end = __( ', ', 'twentytwelve' );
            foreach ( $post_tags as $term ) {
                $term_slug = $term->slug;
                if ( in_array( $term_slug, $primary_tags) ) {
                    if ( isset( $tag_list ) ) {
                        $tag_list .= $tag_end;
                    }
                    $tag_list .= '<a href="' . get_term_link($term_slug, 'post_tag') . 
                        '">' . $term->name . '</a>'; 
                }
            }
        }
    }

    return $tag_list;
}

$primary_tagsを除外したい場合は、代わりに以下を実行してください。

if ( ! in_array( $term_slug, $primary_tags) ) {
    ... 
}

すなわちin_array( ... )の前に!を追加してください。

0
Erk