get_the_tags

Sponsored Link

記事に割り当てられた各タグごとに1つのオブジェクトである、オブジェクトの配列を返します。このタグはループ内で使用する必要があります。

Sponsored Link

Returns an array of objects, one object for each tag assigned to the post. This tag must be used within The Loop.

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

この関数は何も表示されません;オブジェクトにアクセスした時にエコー或いは別の方法では、求められたメンバー変数を使用します。

次の例では、投稿に割り当てられた各タグのタグ名を表示しますが(これはthe_tags()のように使いますが、タグの表示に各タグをリンクせずに、コンマの代わりにスペースを使用します):

This function does not display anything; you should access the objects and then echo or otherwise use the desired member variables.

The following example displays the tag name of each tag assigned to the post (this is like using the_tags(), but without linking each tag to the tag view, and using spaces instead of commas):

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

get_the_tagsのパラメーター

$id

(int) (optional)投稿ID。
デフォルト:なし

(int) (optional) Post id.
Default: None

タグ画像を表示する方法

この出力タグ画像は名前を設定するalt属性にterm_idにちなんで名付けられます。任意の他のメンバー変数も代わりに使うこともできます。

This outputs tag images named after the term_id with the alt attribute set to name. You can also use any of the other member variables instead.

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo '<img src="http://example.com/images/' . $tag->term_id . '.jpg" 
alt="' . $tag->name . '" />'; 
  }
}
?>

最初のタグ名のみ表示する方法

<?php
$posttags = get_the_tags();
$count=0;
if ($posttags) {
  foreach($posttags as $tag) {
    $count++;
    if (1 == $count) {
      echo $tag->name . ' ';
    }
  }
}
?>

異なるタグの値に基づいたコードを表示する方法

このコードは、この投稿が特定のタグ或いはタグ(複数)を持っている場合に応じてHTMLコードを表示します。必要とするif文を多くの他の方法と同様にように追加します。

This code will display HTML code depending on if this post has a certain tag or tag(s). Just add as many else if statements as you require.

<?php 
if ($all_the_tags);
$all_the_tags = get_the_tags();
foreach($all_the_tags as $this_tag) {
	if ($this_tag->name == "sometag" ) {
?>

<p>SOME HTML CODE <img src="someimage.jpg"></p>

<?php 	} else if ($this_tag->name == "someothertag" ) { ?>

<p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p>

<?php 	} else {	
		// it's neither, do nothing
?>
		<!-- not tagged as one or the other -->
<?
	}
}
}
?>

ドロップダウンにタグを表示するための関数

この関数は、ドロップダウンにタグを出力します。

This function outputs tags in a dropdown.

function drop_tags()
{
    echo "<select onChange=\"document.location.href=this.options&#91;this.selectedIndex&#93;.value;\">";
    echo "<option>Tags</option>\n";
    foreach (get_the_tags() as $tag)
    {
        echo "<option value=\"";
        echo get_tag_link($tag->term_id);
        echo "\">".$tag->name."</option>\n";
    }
    echo "</select>";
}

戻り値

(array)
タグオブジェクトの配列

(array)
An array of Tag objects

メンバー変数

term_id

タグID

the tag id

name

タグ名

the tag name

slug

タグ名から生成されるスラッグ

a slug generated from the tag name

term_group

タグのグループがある、いづれかの場合

the group of the tag, if any

taxonomy

この場合いつも’post_tag’でなければなりません

should always be ‘post_tag’ for this case

description

タグ説明文

the tag description

count

このタグの使用回数、合計

number of uses of this tag, total

変更ログ

Since: 2.3.0

関連ファンクションタグ

Tags: the_tags(), tag_description(), single_tag_title(), wp_tag_cloud(), wp_generate_tag_cloud(), get_tags(), get_the_tags(), get_the_tag_list(), get_tag_link()

get_the_terms(), wp_get_object_terms()

Sponsored Link