2022年04月25日

WP関数:get_the_category()

この関数の説明

WP関数の get_the_category() は、投稿IDから[カテゴリ情報]をオブジェクトとして取得します。

 


関数

get_the_category( $id )

 


パラメータ

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

投稿IDを指定します。

省略した場合は、現在の投稿になります。

 


戻り値

カテゴリ情報がオブジェクト形式で返されます。

オブジェクトの中の主要プロパティは下記になります。

プロパティ 名前 解説
cat_ID カテゴリのID(整数) 文書を抽出するWP_Query()で利用できるキーになります

例)'cat' => これで取得した値

slug カテゴリのスラッグ名 文書を抽出するWP_Query()で利用できるキー

例)'category_name' => これで取得した値

name カテゴリの名前 表示されるカテゴリ名
category_parent 親のカテゴリID(整数) 親カテゴリを調べる時に利用します

0:親はありません

n:親のカテゴリIDです

category_count カテゴリの投稿数 このカテゴリの投稿数を表示する時に利用します

 


利用事例

①現在の投稿に設定されているカテゴリ情報を取得します

<?php $cat = get_the_category();?>                <!-- 現在の投稿のカテゴリIDの取得 -->
<?php echo 'cat_count='.count($cat).'<br>';?>  <!-- 設定されているカテゴリ数 -->
<?php
	echo 'スラッグ名='.$cat[0] -> slug.'<br>';          // カテゴリのスラッグ名
	echo 'カテゴリ名='.$cat[0] -> name.'<br>';        // カテゴリ名
	echo '投稿数='.$cat[0] -> category_count.'<br>';// このカテゴリの投稿数
?>
<pre>
  <?php print_r($cat);?>    <!-- カテゴリ情報を出力 -->
</pre>

■1行目:現在の投稿のカテゴリIDを取得します。

■2行目:現在の投稿にカテゴリが幾つ設定されているかをカウントします。

■3~7行目:カテゴリから主要なキーを表示します。

上記は0番目のデータから取得していますが、カテゴリの2番目の場合は[1]になります。

■8~10行目:カテゴリオブジェクトを出力しています。

■結果は下記になります。(参考)

cat_count=1
スラッグ名=wordpress-func
カテゴリ名=WordPress 関数
投稿数=84

  Array
(
    [0] => WP_Term Object
        (
            [term_id] => 3
            [name] => WordPress 関数
            [slug] => wordpress-func
            [term_group] => 0
            [term_taxonomy_id] => 3
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 84
            [filter] => raw
            [cat_ID] => 3
            [category_count] => 84
            [category_description] => 
            [cat_name] => WordPress 関数
            [category_nicename] => wordpress-func
            [category_parent] => 0
        )

)

 


関連情報

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

 

関数一覧
  • 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()