get_the_category

Sponsored Link

オブジェクトの配列を返し、投稿に割り当てられた各カテゴリーのための1つのオブジェクトです。このタグはパラメータとして投稿IDを渡す事によってループ外で使用されます。

Sponsored Link

原文(翻訳元)Returns an array of objects, one object for each category assigned to the post. This tag may be used outside The Loop by passing a post id as the parameter.

get_the_categoryのファンクションタグ使用方法

この関数は何も表示されません。あなたがオブジェクトをアクセスし、echoまたはその他の必要なメンバー変数を使用します。

原文(翻訳元)This function does not display anything; you should access the objects and then echo or otherwise use the desired member variables.

次の例は、記事に割り当てられた各カテゴリーのカテゴリー名を表示します (これはthe_category()を使っているようですが、カテゴリビューに各カテゴリをリンクせず、代わりにコンマのスペースを使用します):

原文(翻訳元)The following example displays the category name of each category assigned to the post (this is like using the_category(), but without linking each category to the category view, and using spaces instead of commas):

<?php
foreach((get_the_category()) as $category) { 
echo $category->cat_name . ' '; 
} 
?>

カテゴリー画像を表示する方法

これは、cat_nameに設定するalt属性と共に、cat_IDの後に名付けられたカテゴリー画像を出力します。代わりに任意の他のメンバ変数を使用することもできます。

原文(翻訳元)This outputs category images named after the cat_ID with the alt attribute set to cat_name. You can also use any of the other member variables instead.

<?php 
foreach((get_the_category()) as $category) { 
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />'; 
} 
?>

最初のカテゴリ名のみ表示する方法

<?php
$category = get_the_category(); 
echo $category&#91;0&#93;->cat_name;
?>

原文(翻訳元)(Echoes the first array ([0]) of $category.)

ループ外から投稿カテゴリーを取得する方法

<?php
global $post;
$categories = get_the_category($post->ID);
var_dump($categories);
?>

メンバー変数

cat_ID

カテゴリーID(’term_id’としても格納されています)

原文(翻訳元)the category id (also stored as ‘term_id’)

cat_name

カテゴリー名(’name’としても格納されています)

原文(翻訳元)the category name (also stored as ‘name’)

category_nicename

カテゴリ名から生成されたスラッグです(’slug’としても格納されています)

原文(翻訳元)a slug generated from the category name (also stored as ‘slug’)

category_description

カテゴリーの説明(’description’としても格納されています)

原文(翻訳元)the category description (also stored as ‘description’)

category_parent

現在のカテゴリーの親のカテゴリーIDです。親がない場合は’0’です(’parent’としても格納されています)。

原文(翻訳元)the category id of the current category’s parent. ‘0’ for no parents. (also stored as ‘parent’)

category_count

このカテゴリの使用数(’count’としても格納されています)

原文(翻訳元)the number of uses of this category (also stored as ‘count’)

関連ファンクションタグ

the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, get_the_category, get_category_parents, get_category_link, is_category, in_category

Sponsored Link