Title: get_postsでループ処理をした後のthe_contentの内容について
Last modified: 2009年5月24日

---

# get_postsでループ処理をした後のthe_contentの内容について

 *  [kopy](https://ja.wordpress.org/support/users/kopy/)
 * (@kopy)
 * [17年、 5ヶ月前](https://ja.wordpress.org/support/topic/get_posts%e3%81%a7%e3%83%ab%e3%83%bc%e3%83%97%e5%87%a6%e7%90%86%e3%82%92%e3%81%97%e3%81%9f%e5%be%8c%e3%81%aethe_content%e3%81%ae%e5%86%85%e5%ae%b9%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/)
 * 初めて投稿させていただきます。
 * アーカイブ(archive.php)において、カテゴリーID10にget_postsを用いてカテゴリーID8
   のカスタムフィールドをループ表示します。
 * その後the_contentでループを表示しているカテゴリーID10の記事内容を表示させると、
   内容がID8のthe_contentになってしまいます。
 * ID8のカスタムフィールドのループ前にthe_contentを置くとID10の内容が表示できるので
   postのIDがリセットされないのが原因だと思うのですが、どうもやりかたがわかりません。
 * ループをリセットするためにrewind_posts()を使ったりいろいろ試したのですが、どれ
   もだめでした。
 * どなたかご教授いただけませんでしょか？
 * 以下が、そのソースとなります。
 *     ```
       <?php if (is_category('10')): ?>
   
       <?php the_title(); ?>
   
       <?php
       $posts = get_posts('numberposts=100&category=8');
       global $post;
       ?>
       <?php
       if($posts): foreach($posts as $post): setup_postdata($post); ?>
   
       <p class="Price">￥<?php echo get_post_meta($post->ID,'プラン値段',TRUE);?></p>
   
       <p><?php echo get_post_meta($post->ID,'プラン特徴',TRUE);?></p>
   
       <?php endforeach; endif; ?>
   
       <?php the_content(); ?>//←ここの表示をID10にしたいのですがID8になってしまいます。
   
       <?php endif; ?>
       ```
   

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

 *  [shokun0803](https://ja.wordpress.org/support/users/shokun0803/)
 * (@shokun0803)
 * [17年、 5ヶ月前](https://ja.wordpress.org/support/topic/get_posts%e3%81%a7%e3%83%ab%e3%83%bc%e3%83%97%e5%87%a6%e7%90%86%e3%82%92%e3%81%97%e3%81%9f%e5%be%8c%e3%81%aethe_content%e3%81%ae%e5%86%85%e5%ae%b9%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-118119)
 * $postsの内容をリセットしたいのは私もやりたくてできなかったので退避させる方法を
   使用しています。例として
 *     ```
       $my_posts = $posts;
       $posts = get_posts(・・・略
   
       $posts = $my_posts;
       ```
   
 * こんな感じです。過去のフォーラムに似たような記事があって参考にしました。
    正直
   私もループ内で$postsの内容をリセットする方法を知りたいです（汗）
 *  トピック投稿者 [kopy](https://ja.wordpress.org/support/users/kopy/)
 * (@kopy)
 * [17年、 5ヶ月前](https://ja.wordpress.org/support/topic/get_posts%e3%81%a7%e3%83%ab%e3%83%bc%e3%83%97%e5%87%a6%e7%90%86%e3%82%92%e3%81%97%e3%81%9f%e5%be%8c%e3%81%aethe_content%e3%81%ae%e5%86%85%e5%ae%b9%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-118173)
 * shokun0803様
 * ご返信ありがとうございます。
    気づくのが遅くて申し訳ございませんでした。
 * 上記を試させていただいたのですが、やはりID8の内容が表示されてしまいます。
 *     ```
       <?php if (is_category('10')): ?>
   
       <?php the_title(); ?>
   
       <?php
       $my_posts = $posts;
       $posts = get_posts('numberposts=100&category=8');
       global $post;
       ?>
       <?php
       if($posts): foreach($posts as $post): setup_postdata($post); ?>
   
       <p class="Price">￥<?php echo get_post_meta($post->ID,'プラン値段',TRUE);?></p>
   
       <p><?php echo get_post_meta($post->ID,'プラン特徴',TRUE);?></p>
   
       <?php endforeach; endif; ?>
   
       <?php $posts = $my_posts; ?>
       <?php the_content(); ?>//←ここの表示をID10にしたいのですがID8になってしまいます。
   
       <?php endif; ?>
       ```
   
 * 書き方間違っていますでしょうか？
 * それともしご面倒でなければ「過去のフォーラムの似たような記事」のアドレスもお教え
   願えませんでしょうか？
 * 不躾な質問で申し訳ございません。
 * ご教授の程よろしくお願いいたします。
 *  [jim912](https://ja.wordpress.org/support/users/jim912/)
 * (@jim912)
 * [17年、 5ヶ月前](https://ja.wordpress.org/support/topic/get_posts%e3%81%a7%e3%83%ab%e3%83%bc%e3%83%97%e5%87%a6%e7%90%86%e3%82%92%e3%81%97%e3%81%9f%e5%be%8c%e3%81%aethe_content%e3%81%ae%e5%86%85%e5%ae%b9%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-118174)
 * kopyさん、こんにちは。
 * get_postsを利用されるのであれば、$postsと$postという変数名を使わなければ問題ありま
   せん。
 *     ```
       <?php if (is_category('10')): ?>
       <?php the_title(); ?>
       <?php
       $plan_posts = get_posts('numberposts=100&category=8');
       if($plan_posts): foreach($plan_posts as $plan_post): ?>
   
       <p class="Price">￥<?php echo get_post_meta($plan_post->ID,'プラン値段',TRUE);?></p>
       <p><?php echo get_post_meta($plan_post->ID,'プラン特徴',TRUE);?></p>
       <?php endforeach; endif; ?>
       <?php the_content(); ?>
       <?php endif; ?>
       ```
   
 *  [makinos](https://ja.wordpress.org/support/users/makinos/)
 * (@makinos)
 * [12年、 3ヶ月前](https://ja.wordpress.org/support/topic/get_posts%e3%81%a7%e3%83%ab%e3%83%bc%e3%83%97%e5%87%a6%e7%90%86%e3%82%92%e3%81%97%e3%81%9f%e5%be%8c%e3%81%aethe_content%e3%81%ae%e5%86%85%e5%ae%b9%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-118246)
 * jim912さん
 * 同様の現象で困っていました。
    jim912さんの回避方法ですんなり解決しました。
 * 変数名はどうしても類似してくるので気をつけます。
    ありがとうございました。

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

 トピック「get_postsでループ処理をした後のthe_contentの内容について」には新たに返信
することはできません。

## タグ

 * [ループ](https://ja.wordpress.org/support/topic-tag/%e3%83%ab%e3%83%bc%e3%83%97/)
 * [解除](https://ja.wordpress.org/support/topic-tag/%e8%a7%a3%e9%99%a4/)
 * [get_posts](https://ja.wordpress.org/support/topic-tag/get_posts/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4件の返信
 * 4人の参加者
 * 最後の返信者: [makinos](https://ja.wordpress.org/support/users/makinos/)
 * 最後の活動: [12年、 3ヶ月前](https://ja.wordpress.org/support/topic/get_posts%e3%81%a7%e3%83%ab%e3%83%bc%e3%83%97%e5%87%a6%e7%90%86%e3%82%92%e3%81%97%e3%81%9f%e5%be%8c%e3%81%aethe_content%e3%81%ae%e5%86%85%e5%ae%b9%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-118246)
 * 状態: 未解決

## トピック

### 人気のトピック

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

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

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

### 未解決のトピック

### すべてのトピック
