Title: ループ内でタグが取得できない
Last modified: 2016年9月15日

---

# ループ内でタグが取得できない

 *  [kemusi](https://ja.wordpress.org/support/users/kemusi/)
 * (@kemusi)
 * [11年、 2ヶ月前](https://ja.wordpress.org/support/topic/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%82%bf%e3%82%b0%e3%81%8c%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84/)
 * 初心者ですが、ご質問させてください！
 * ショートコードにて
    下記の内容で出力したのですが `get_the_tags()`だけが　Array
   と 表示されて取得できません
 * ご教授をお願いいたします。
 *     ```
       function show_Cat_Posts_func($atts) {
       	global $post;
       	$output = "";
       	extract(shortcode_atts(array(
       		'cat' => 1,// デフォルトカテゴリーID = 1
       		'show' => 6, // デフォルト表示件数 = 3
       	), $atts));
   
       	$cat = rtrim($cat, ",");
       	// get_postsで指定カテゴリーの記事を指定件数取得
       	$args = array(
       		'cat' => $cat,
       		'posts_per_page' => $show
       	);
       	$my_posts = get_posts($args);
   
       	// 上記条件の投稿があるなら$outputに出力、マークアップはお好みで
       	if ($my_posts) {
       		// カテゴリーを配列に
       		$cat = explode(",", $cat);
       		$catnames = "";
       		foreach ($cat as $catID) : // カテゴリー名取得ループ
       		$catnames .= get_the_category_by_ID($catID).", ";
       		endforeach;
       		$catnames = rtrim($catnames, ", ");
       		$output .= '<aside class="showcatposts">'."\n";
       		$output .= '<ul class="showcatposts-list clearfix">'."\n";
       		foreach ($my_posts as $post) : // ループスタート
       			setup_postdata($post); // get_the_title() などのテンプレートタグを使えるようにする
       			$output .= '<li id="post-'.get_the_ID().'" '.get_post_class().'>'.get_the_tags().'<span>'.get_post_time('Y.m.d').'<a href="'.get_permalink().'">'.get_the_post_thumbnail().'</a><br></span> <a href="'.get_permalink().'">'.get_the_title().'<p style="color:#568485;">'.get_the_excerpt().get_the_tags('id')."</p></a></li>\n";
       		endforeach; // ループ終わり
       		$output .= "</ul>\n";
       		$output .= "</aside>\n";
   
       	}
       	// クエリのリセット
       	wp_reset_postdata();
       	return $output;
       }
       add_shortcode('showcatposts', 'show_Cat_Posts_func');
       ```
   

4件の返信を表示中 - 1 - 4件目 (全4件中)

 *  [Toshimichi Mimoto](https://ja.wordpress.org/support/users/mimosafa/)
 * (@mimosafa)
 * [11年、 2ヶ月前](https://ja.wordpress.org/support/topic/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%82%bf%e3%82%b0%e3%81%8c%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84/#post-196293)
 * `get_the_tags()` の返り値はタグオブジェクトの配列です。そのまま`echo` できません。
   
   [こちら](http://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/get_the_tags#Usage)
   をご覧ください。
 *  トピック投稿者 [kemusi](https://ja.wordpress.org/support/users/kemusi/)
 * (@kemusi)
 * [11年、 2ヶ月前](https://ja.wordpress.org/support/topic/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%82%bf%e3%82%b0%e3%81%8c%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84/#post-196303)
 * mimosafa様
 * 早速ありがとうございます
 *     ```
       $posttags = get_the_tags();
       if ( $posttags[0] ) {
       echo $posttags[0]->name;
       }
       ```
   
 * を追加してみましたが、今度はArrayすら出なくなってしまいました。
    何か記述を間
   違っているのでしょうか？
 *     ```
       function show_Cat_Posts_func($atts) {
       	global $post;
       	$output = "";
       	extract(shortcode_atts(array(
       		'cat' => 108,107,106,// デフォルトカテゴリーID = 1
       		'show' => 6, // デフォルト表示件数 = 3
       	), $atts));
       	$cat = rtrim($cat, ",");
       	// get_postsで指定カテゴリーの記事を指定件数取得
       	$args = array(
       		'cat' => $cat,
       		'posts_per_page' => $show
       	);
       	$my_posts = get_posts($args);
   
       	// 上記条件の投稿があるなら$outputに出力、マークアップはお好みで
       	if ($my_posts) {
       		// カテゴリーを配列に
       		$cat = explode(",", $cat);
       		$catnames = "";
       		foreach ($cat as $catID) : // カテゴリー名取得ループ
       		$catnames .= get_the_category_by_ID($catID).", ";
       		endforeach;
       		$catnames = rtrim($catnames, ", ");
       ```
   
 * $posttags = get_the_tags();
    if ( $posttags[0] ) { echo $posttags[0]->name; }
   $output .= ‘<aside class=”showcatposts”>’.”\n”; $output .= ‘<ul class=”showcatposts-
   list clearfix”>’.”\n”; foreach ($my_posts as $post) : // ループスタート setup_postdata(
   $post); // get_the_title() などのテンプレートタグを使えるようにする $output .
   = ‘<li id=”post-‘.get_the_ID().'”><span>’.$posttags.get_post_time(‘Y.m.d’).’[‘.get_the_post_thumbnail().’](https://ja.wordpress.org/support/topic/%E3%83%AB%E3%83%BC%E3%83%97%E5%86%85%E3%81%A7%E3%82%BF%E3%82%B0%E3%81%8C%E5%8F%96%E5%BE%97%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84/&apos;.get_permalink().&apos;?output_format=md)
   </span> [<p class=”newstitle”>’.get_the_title().'</p><p style=”color:#568485; margin:10px 0 0 !important; line-height:1em;”>’.get_the_excerpt().”</p>](https://ja.wordpress.org/support/topic/%E3%83%AB%E3%83%BC%E3%83%97%E5%86%85%E3%81%A7%E3%82%BF%E3%82%B0%E3%81%8C%E5%8F%96%E5%BE%97%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84/&apos;.get_permalink().&apos;?output_format=md)\
   n”; endforeach; // ループ終わり $output .= “\n”; $output .= “</aside>\n”;
 *  }
    // クエリのリセット wp_reset_postdata(); return $output; } add_shortcode(‘
   showcatposts’, ‘show_Cat_Posts_func’);`
 *  [Toshimichi Mimoto](https://ja.wordpress.org/support/users/mimosafa/)
 * (@mimosafa)
 * [11年、 2ヶ月前](https://ja.wordpress.org/support/topic/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%82%bf%e3%82%b0%e3%81%8c%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84/#post-196305)
 * 私が
 * > そのままecho できません。
 * なんて言ってしまったから誤解を招いてしまったと思います… スミマセン
 * `$my_posts`を`foreach`でループさせている部分で`setup_postdata`後に
 *     ```
       $tags = '';
       $posttags = get_the_tags();
       if ( $posttags ) {
       	foreach ( $posttags as $tag ) {
       		$tags .= $tag->name . ' ';
       	}
       	$tags = trim( $tags );
       }
       ```
   
 * という感じで表示させたいタグ文字列を適当な変数に格納して下さい。
    そして`get_the_tags()`
   となっている部分をその変数（上記の場合`$tags`）に置き換えてみてください。
 *  トピック投稿者 [kemusi](https://ja.wordpress.org/support/users/kemusi/)
 * (@kemusi)
 * [11年、 2ヶ月前](https://ja.wordpress.org/support/topic/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%82%bf%e3%82%b0%e3%81%8c%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84/#post-196307)
 * mimosafa様
 * うわ！できました！ありがとうございます！！！！
    親切に解説までつけていただき大変
   分りやすかったです。
 * なんでこうなったかもう少し勉強が必要ですが
    大変参考になりました！
 * 本当にありがとうございます。

4件の返信を表示中 - 1 - 4件目 (全4件中)

 トピック「ループ内でタグが取得できない」には新たに返信することはできません。

## タグ

 * [ショートコード](https://ja.wordpress.org/support/topic-tag/%e3%82%b7%e3%83%a7%e3%83%bc%e3%83%88%e3%82%b3%e3%83%bc%e3%83%89/)
 * [タグ出力](https://ja.wordpress.org/support/topic-tag/%e3%82%bf%e3%82%b0%e5%87%ba%e5%8a%9b/)
 * [タグ名](https://ja.wordpress.org/support/topic-tag/%e3%82%bf%e3%82%b0%e5%90%8d/)
 * [ループ内](https://ja.wordpress.org/support/topic-tag/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85/)
 * [get_the_tags](https://ja.wordpress.org/support/topic-tag/get_the_tags/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4件の返信
 * 2人の参加者
 * 最後の返信者: [kemusi](https://ja.wordpress.org/support/users/kemusi/)
 * 最後の活動: [11年、 2ヶ月前](https://ja.wordpress.org/support/topic/%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%82%bf%e3%82%b0%e3%81%8c%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84/#post-196307)
 * 状態: 未解決

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
