2022年04月25日

WP関数:the_category()

この関数の説明

WP関数の the_category() は、現在の投稿に設定されているカテゴリをリンク付きで表示する関数です。

 


関数

the_category( $separator, $parents, $post_id )

 


パラメータ

$separator(文字列) (オプション)

複数のカテゴリが選択されている場合、個々のカテゴリを区切る文字列を指定します。

' | 'の場合は、カテゴリA | カテゴリB で表示されます。

省略か ''(空白)を指定した場合は、<ul><li>のリスト表示になります。

 

$parents(文字列) (オプション)

選択されたカテゴリに親がある場合、親カテゴリの出力の仕方を決めます。

指定値 挙動
省略又は '' 親は表示しません
'single' 親子を$separatorで区切って表示し、リンクは全体で子供にリンクします
'multiple' 親子を$separatorで区切って表示し、リンクは親と子で別々にリンクします

 

$post_id(整数) (オプション)

カテゴリを取得する投稿のIDを指定します。

 


事例

①設定されているカテゴリを表示する

下記は投稿文書に設定されているカテゴリを表示するサンプルです。

<?php the_category(' | ' );?>

■カテゴリの[WordPress関数]と[WPアクション]にチェックが付いている場合は、下図の様に表示されます。

■どちらかのカテゴリに親があっても無視されます。

 

②カテゴリの親を含めて表示する

下記はパンくずの様な形で表示させたサンプルです。

<?php 
   the_category(' / ','multiple' );  // カテゴリを表示
   echo ' / ' ;                              // 区切り文字の出力
   the_title();                             // 文書タイトルを表示
?>

■投稿のカテゴリにWordPress関数にチェックが付いており、このカテゴリに親がある場合は下図の様に表示されます。

 


関連情報

関連関数は カテゴリ関連関数 を参照してください。

 

 

関数一覧
  • 1.WPのエスケープ処理
  • 2.WPプログラムの基本
  • 3.ヘッダ/フッタ/サイドバで使われる関数
  • 4.DBからの読込関数
  • 5.一般テンプレートの中で使われる関数
  • 6.その他のWP関数
  • add_shortcode()
  • bloginfo()
  • body_class()
  • date_i18n()
  • dynamic_sidebar()
  • edit_post_link()
  • esc_attr()
  • esc_html_e()
  • esc_html()
  • esc_url()
  • get_ancestors()
  • get_cat_ID()
  • get_cat_name()
  • get_category_link()
  • get_category_parents()
  • get_category()
  • get_footer()
  • get_header()
  • get_home_url()
  • get_post_format()
  • get_post_meta()
  • get_post_thumbnail_id()
  • get_query_var()
  • get_search_query()
  • get_sidebar()
  • get_site_url()
  • get_stylesheet_directory_uri()
  • get_stylesheet_uri()
  • get_template_directory_uri()
  • get_template_part()
  • get_the_category()
  • get_the_content()
  • get_the_date()
  • get_the_excerpt()
  • get_the_ID()
  • get_the_modified_date()
  • get_the_modified_time()
  • get_the_permalink()
  • get_the_post_thumbnail()
  • get_the_tags()
  • get_the_time()
  • get_the_title()
  • get_uploaded_header_images()
  • get_year_link()
  • has_category()
  • has_custom_logo()
  • has_header_image()
  • has_header_video()
  • has_nav_menu()
  • has_post_thumbnail()
  • has_tag()
  • header_image()
  • home_url()
  • is_admin()
  • is_category()
  • is_date()
  • is_front_page()
  • is_home()
  • is_main_query()
  • is_page()
  • is_search()
  • is_single()
  • language_attributes()
  • the_category()
  • the_content()
  • the_custom_header_markup()
  • the_custom_logo()
  • the_date()
  • the_excerpt()
  • the_ID()
  • the_modified_date()
  • the_modified_time()
  • the_permalink()
  • the_post_thumbnail()
  • the_search_query()
  • the_tags()
  • the_time()
  • the_title()
  • update_post_meta()
  • wp_get_attachment_image_src()
  • wp_nav_menu()
  • WP_Query()