アーカイブ

投稿日:2020年7月15日 更新日:

wordpressでカテゴリー名を出力

archive.phpでカテゴリ名を表示したい時

カテゴリーページ(archive.php)にカテゴリ名を出力したいときは これが一番シンプルで簡単です。 (ループの外で以下を使用)

<?php single_cat_title(); ?>

投稿ページ(single.php)で表示したい時

投稿ページで表示したい時は、get_the_category()を使用します。

以下はリンク付で

<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( $categories ) {
	foreach( $categories as $category ) {
		$output .= '<a href="' . get_category_link( $category->term_id ) . '" title="' 
			. esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) 
			. '">' . $category->cat_name . '</a>' . $separator;
	}
echo trim( $output, $separator );
}
?>

参考:WP Codex日本語版[テンプレートタグ/get the category]

- アーカイブ

関連記事

no image

the_excerpt();の文字数変更

functions.phpに記載 以下のコードをテーマのfunctions.phpにコピーします。 function twpp_change_excerpt_length( $length ) { r ...