サポート » 使い方全般 » ループ内でタグが取得できない

  • 初心者ですが、ご質問させてください!

    ショートコードにて
    下記の内容で出力したのですが
    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件中)
  • get_the_tags() の返り値はタグオブジェクトの配列です。そのままecho できません。
    こちらをご覧ください。

    トピック投稿者 kemusi

    (@kemusi)

    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().’
    </span> <p class=”newstitle”>’.get_the_title().'</p><p style=”color:#568485; margin:10px 0 0 !important; line-height:1em;”>’.get_the_excerpt().”</p>\n”;
    endforeach; // ループ終わり
    $output .= “\n”;
    $output .= “</aside>\n”;

    }
    // クエリのリセット
    wp_reset_postdata();
    return $output;
    }
    add_shortcode(‘showcatposts’, ‘show_Cat_Posts_func’);`

    私が

    そのままecho できません。

    なんて言ってしまったから誤解を招いてしまったと思います… スミマセン

    $my_postsforeachでループさせている部分でsetup_postdata後に

    $tags = '';
    $posttags = get_the_tags();
    if ( $posttags ) {
    	foreach ( $posttags as $tag ) {
    		$tags .= $tag->name . ' ';
    	}
    	$tags = trim( $tags );
    }

    という感じで表示させたいタグ文字列を適当な変数に格納して下さい。
    そしてget_the_tags()となっている部分をその変数(上記の場合$tags)に置き換えてみてください。

    トピック投稿者 kemusi

    (@kemusi)

    mimosafa様

    うわ!できました!ありがとうございます!!!!
    親切に解説までつけていただき大変分りやすかったです。

    なんでこうなったかもう少し勉強が必要ですが
    大変参考になりました!

    本当にありがとうございます。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「ループ内でタグが取得できない」には新たに返信することはできません。