ショートコードの練習 – タームを全部出す

ワードプレス

今までショートコードはほとんど使わなかったので、初歩的なことから練習中。

文中、[]は「」に置き換えています。

ショートコードが動いちゃったり、バリデーションで書き換えられたりするので。

また、記事が多い順に並ぶようにしていますが、タクソノミーの並べ替えプラグインとか入っていると、そっちが優先されます。

全ての投稿タイプ、全てのタクソノミ、全てのタームをすべて出す。

//すべてのタームを吐き出す
function show_all_terms( $atts ){
	$args = array(
		'public'	=> true,// 公開された(publicな)投稿タイプのみ
//		'_builtin'	=> false,//条件なしで全ての投稿タイプ、trueでデフォルトの投稿タイプのみ、falseでデフォルトの投稿タイプを除外
	);
	$output_type = 'names'; // names or objects, note names is the default
	$operator = 'and'; // 'and' or 'or'
	$post_types = get_post_types( $args, $output_type, $operator );
	$output .= '<p class="taglist taglist-all">';
	if( !empty( $atts['tax'] ) ){//タクソノミー指定
		if( !empty( get_terms( $atts['tax'] ) ) ){
			$output .= '<span class="taglist-tax taglist-tax-' . $atts['tax'] . ' tax-order-1">';
			$args = array(
				'taxonomy'		=> $atts['tax'],
				'orderby'		=> 'count',
				'order'			=> 'DESC',
				'hide_empty'	=> 1,
				'pad_counts'	=> 0,
			);
			$terms = get_terms( $args );
			foreach ( $terms as $term ){
				$output .= '<a href="' . get_term_link( $term ) . '" class="taglist-term taglist-term-' . esc_html( $term->slug ) . '">' . $term->name . '(' . $term->count .')' . '</a>';
			}
			$output .= '</span>';
		}else{
			$output .= '<span>タクソノミー "' . $atts['tax'] . '" にはタームがありません。</span>';
		}
	}elseif( !empty( $atts['post_type'] ) ){//投稿タイプ指定
		$taxonomies = get_taxonomies( array( 'object_type' => array( $atts['post_type'] ) ) );
		if( !empty( get_terms( $taxonomies ) ) ){
			$output .= '<span class="taglist-posttype taglist-posttype-' . $atts['post_type'] . '">';
			$count=1;
			foreach( $taxonomies as $tax ){
				$output .= '<span class="taglist-tax taglist-tax-' . $tax . ' tax-order-' . $count . '">';
				$args = array(
					'taxonomy'		=> $tax,
					'orderby'		=> 'count',
					'order'			=> 'DESC',
					'hide_empty'	=> 1,
					'pad_counts'	=> 0,
				);
				$terms = get_terms( $args );
				foreach ( $terms as $term ){
					$output .= '<a href="' . get_term_link( $term ) . '" class="taglist-term taglist-term-' . esc_html( $term->slug ) . '">' . $term->name . '(' . $term->count .')' . '</a>';
				}
				$output .= '</span>';
			$count++;
			}
			$output .= '</span>';
		}else{
			$output .= '<span>投稿タイプ "' . $atts['post_type'] . '" にはタームがありません。</span>';
		}
	}else{//条件なしで全部
		foreach( $post_types as $post_type ){
			if( $post_type == 'page' || $post_type == 'attachment' ){
			}else{
				$taxonomies = get_taxonomies( array( 'object_type' => array( $post_type ) ) );
				if( !empty( get_terms( $taxonomies ) ) ){
					$output .= '<span class="taglist-posttype taglist-posttype-' . $post_type . '">';
					$count=1;
					foreach( $taxonomies as $tax ){
						$output .= '<span class="taglist-tax taglist-tax-' . $tax . ' tax-order-' . $count . '">';
						$args = array(
							'taxonomy'		=> $tax,
							'orderby'		=> 'count',
							'order'			=> 'DESC',
							'hide_empty'	=> 1,
							'pad_counts'	=> 0,
						);
						$terms = get_terms( $args );
						foreach ( $terms as $term ){
							$output .= '<a href="' . get_term_link( $term ) . '" class="taglist-term taglist-term-' . esc_html( $term->slug ) . '">' . $term->name . '(' . $term->count .')' . '</a>';
						}
						$output .= '</span>';
					$count++;
					}
					$output .= '</span>';
				}
			}
		}
	}
	$output .= '</p>';
	return $output;
}
add_shortcode( 'show-all-terms', 'show_all_terms' );

オプション有り無しの処理で大分長くなっていて、ぎこちないです。

ループ処理の外側が違うと、コンパクトにするのが難しい。

 

投稿タイプ「page」と「attachment」は除外しています。

タームを持たない投稿タイプは除外が難しかったので、ループに入ってきて無駄なhtmlを吐きます。

2019/08/02

  • タームを持たない場合はループに入らないよう処理を追加、
  • 投稿タイプ、タクソノミーを指定した場合は、「タームが無いよ」のメッセージを表示する

ように変更。

 

また、「最初のタクソノミーのタームは色付き」などCSSを書きやすいように自動連番を振ってます。

tax-order-1、tax-order-2、、、

echo / return

今回、「echo」を「return」に置き換える練習として作りました。

この例は「echo」のままでも動きますが、「return」を使わないとNGなケースも結構あるようです。

「echo」で小出しにせず、「.=」でどんどん溜めていって、最後に「return」で放出すればいいんだな、という風に覚えました。

オプション

例:とにかく全部出す。

「show-all-terms」

ワードプレス(79)ガジェット(33)ワードプレス(60)関数ファイル(19)PHP(33)HTML(7)CSS(11)Java-script(8)Mac OS(1)手動インストール(1)IE(1)クローム(2)iOS(4)Lightbox(1)MW WP Form(6)WordPress Popular Posts(1)アドバンスドカスタムフィールド(5)アニメーション(3)ウィジェット(1)カスタムフィールド(13)カスタム投稿タイプ(10)サブループ(5)サーバー(6)ショートコード(9)タクソノミー(9)タグ、カテゴリー(8)ターム(8)テンプレートタグ(11)ビジュアルエディタ(4)ファイアーウォール(1)プラグイン(19)メールフォーム(6)動画(1)変数(1)改行(3)条件分岐(8)管理画面(12)絵文字(1)著者(1)要約(3)関数(7)

 

「post_format」って「post」の第3のタクソノミーだったんだな、とか見えてきます。

 

オプションを付けてやると、投稿タイプ、タクソノミーを指定できます。

「show-all-terms post_type=”カスタム投稿タイプ名”」

「show-all-terms tax=”タクソノミー名”」

例:投稿タイプ「メモ」のタクソノミ「memo_cat」を出力

「show-all-terms post_type=”memo” tax=”memo_cat”」

[show-all-terms post_type=”memo” tax=”memo_cat”]

使いどころ

標準のウィジェットには、「カテゴリ」「アーカイブ」しかなく、カスタム投稿に対応したターム一覧表示はない。

「タグクラウド」はカスタム投稿に対応しているけれど、タクソノミで指定するので、「全部のターム」は出せない。

 

「カスタム投稿タイプは”news”しかないので、そこにだけタームリストがあればいい」

というような場合ならば、

「show-all-terms post_type=”news”」

と書けば済みます。

 

投稿タイプ別に、持ってるタームを全部出すならば、

テンプレートファイルに

$post_type = get_post_type();

echo do_shortcode( ‘「show-all-terms post_type = “‘ . $post_type . ‘”」’ );

という感じで該当投稿タイプが持つタームが全部出ます。

「.」での連結が必要

 

ま~、こういう機能ならプラグインが山ほどあるし、

投稿タイプがいろいろあるなら、テンプレートファイルに直接処理を書いた方が自動化出来るし、細かく調整も出来るので、「とにかく全部出す!」場合限定な気がします。

 

自分の書いたメモを探すときには便利だと思うので、このサイトではフッターにドバーっと出してみました。