Title: 関数the_terms()の出力位置が希望通りにならない
Last modified: 2021年1月15日

---

# 関数the_terms()の出力位置が希望通りにならない

 *  [no23h](https://ja.wordpress.org/support/users/no23h/)
 * (@no23h)
 * [5年、 8ヶ月前](https://ja.wordpress.org/support/topic/%e9%96%a2%e6%95%b0the_terms%e3%81%ae%e5%87%ba%e5%8a%9b%e4%bd%8d%e7%bd%ae%e3%81%8c%e5%b8%8c%e6%9c%9b%e9%80%9a%e3%82%8a%e3%81%ab%e3%81%aa%e3%82%89%e3%81%aa%e3%81%84/)
 * functions.phpにカスタム投稿のリストを出力させるショートコードを作成しました。
 *     ```
       function shortcode_post_list($atts) {
         extract(shortcode_atts(array(
           'num' => '-1',
           'post_type' => 'post',
         ), $atts));
   
         global $post;
         $args = array(
           'posts_per_page' => $num,
           'post_type'      => $post_type,
           'order'          => 'DESC',
           'post_status'    => 'publish'
         );
         $posts_array = get_posts($args);
         $html = '<ul>';
         foreach($posts_array as $post):
           setup_postdata($post);
           $html .= '<li>';
           $html .= '<a href="'.get_permalink().'">';
   
           if (has_post_thumbnail()) :
             $html .= '<div style="background-image: url('.wp_get_attachment_url( get_post_thumbnail_id() ).')"></div>';
           else :
             $html .= '<div style="background-image: url('.get_theme_file_uri().'/img/eyecatch_default.jpg)"></div>';
           endif;
           $html .= '</a>';
           $html .= '<p>';
           $html .= '<span>'.get_the_date().'</span>';
           $html .= '<span class="category">'.the_terms(get_the_ID(),'news_cat').'</span>';
           $html .= '</p>';
           $html .= '<h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>';
           $html .= '</li>';
   
         endforeach;
         $html.='</ul>';
         wp_reset_postdata();
         return $html;
       }
       add_shortcode('post_list', 'shortcode_post_list');
       ```
   
 * これを、プラグイン「Elementor」のショートコードウィジェットで出力したところ、
   以下のようにthe_terms()の出力位置がショートコードウィジェットの外になってしま
   います。
     [出力結果] (「…」の部分は長いので省略)
 *     ```
       <div class="elementor-widget-container">
   
       　※タームがこの位置に出力されてしまう
         <a href="..." rel="tag">ニュースカテゴリ2</a>
         <a href="..." rel="tag">ニュースカテゴリ1</a>
   
         <div class="elementor-shortcode">
             <ul>
                 <li><a href="...">
                         <div style="..."></div>
                     </a>
                     <p><span>2021年1月8日</span><span class="category"></span></p>
                     <h3><a href="...">タイトル2</a></h3>
                 </li>
                 <li><a href="...">
                         <div style="..."></div>
                     </a>
                     <p><span>2021年1月7日</span><span class="category"></span></p>
                     <h3><a href="...">タイトル1</a></h3>
                 </li>
             </ul>
         </div>
       </div>
       ```
   
 * また、テンプレートタグに直接ショートコードを書き込んだ場合、
    - タグの外に出力されてしまいます。
       `<?php echo do_shortcode('[post_list post_type
      = "news" num = "2" ]'); ?>`
    - [出力結果]
    -     ```
           <a href="..." rel="tag">ニュースカテゴリ2</a>
          <a href="..." rel="tag">ニュースカテゴリ1</a>
          <ul>
              <li><a href="...">
                      <div style="..."></div>
                  </a>
                  <p><span>2021年1月8日</span><span class="category"></span></p>
                  <h3><a href="...">タイトル2</a></h3>
              </li>
              <li><a href="...">
                      <div style="..."></div>
                  </a>
                  <p><span>2021年1月8日</span><span class="category"></span></p>
                  <h3><a href="...">タイトル1</a></h3>
              </li>
          </ul>
          ```
      
    - the_terms()がショートコード内で機能していないような印象を受けますが、原因は
      何でしょうか？
       お詳しい方、ご教授いただけますと幸いです。
    - 宜しくお願い致します。

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

 *  [ishitaka](https://ja.wordpress.org/support/users/ishitaka/)
 * (@ishitaka)
 * [5年、 8ヶ月前](https://ja.wordpress.org/support/topic/%e9%96%a2%e6%95%b0the_terms%e3%81%ae%e5%87%ba%e5%8a%9b%e4%bd%8d%e7%bd%ae%e3%81%8c%e5%b8%8c%e6%9c%9b%e9%80%9a%e3%82%8a%e3%81%ab%e3%81%aa%e3%82%89%e3%81%aa%e3%81%84/#post-11669013)
 * こんにちは
 * [the_terms()](https://developer.wordpress.org/reference/functions/the_terms/)
   は結果の文字列を表示（echo）します。文字列で取得する場合は、下記のようにすると
   いいかと思います。
 * `the_terms(get_the_ID(),'news_cat')`
    ↓ `get_the_term_list( get_the_ID(), 'news_cat','',',','')`
 *  トピック投稿者 [no23h](https://ja.wordpress.org/support/users/no23h/)
 * (@no23h)
 * [5年、 8ヶ月前](https://ja.wordpress.org/support/topic/%e9%96%a2%e6%95%b0the_terms%e3%81%ae%e5%87%ba%e5%8a%9b%e4%bd%8d%e7%bd%ae%e3%81%8c%e5%b8%8c%e6%9c%9b%e9%80%9a%e3%82%8a%e3%81%ab%e3%81%aa%e3%82%89%e3%81%aa%e3%81%84/#post-11669029)
 * [@ishitaka](https://ja.wordpress.org/support/users/ishitaka/) 様
 * ご回答いただき有難うございます。
    get_the_term_listでうまくいきました。

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

 トピック「関数the_terms()の出力位置が希望通りにならない」には新たに返信すること
はできません。

## タグ

 * [PHP](https://ja.wordpress.org/support/topic-tag/php/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2件の返信
 * 2人の参加者
 * 最後の返信者: [no23h](https://ja.wordpress.org/support/users/no23h/)
 * 最後の活動: [5年、 8ヶ月前](https://ja.wordpress.org/support/topic/%e9%96%a2%e6%95%b0the_terms%e3%81%ae%e5%87%ba%e5%8a%9b%e4%bd%8d%e7%bd%ae%e3%81%8c%e5%b8%8c%e6%9c%9b%e9%80%9a%e3%82%8a%e3%81%ab%e3%81%aa%e3%82%89%e3%81%aa%e3%81%84/#post-11669029)
 * 状態: 未解決

## トピック

### 人気のトピック

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

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

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

### 未解決のトピック

### すべてのトピック
