• 解決済 regulus-design

    (@regulus-design)


    トップページの記事一覧をカテゴリーID55の記事は除外して
    おすすめ記事(この投稿を先頭に固定表示にチェックした記事)
    大見出し:最新記事2件
    小見出し:最新記事3〜8件
    という表示にしたいので、下記のようなコードを打ちましたが、
    最新記事とおすすめ記事に同一記事があると、どちらも出力されてしまいます。

    <div class="row">
      <?php
        $myquery_pickup = array(
    	'posts_per_page' => 1,
    	'ignore_sticky_posts' => 1,
    	'post__in' => get_option('sticky_posts')
      ); ?>
        <?php query_posts($myquery_pickup); ?>
        <?php if(have_posts()): while(have_posts()): the_post(); ?>
    	<?php get_template_part('content','pickup'); ?>
        <?php endwhile; endif; ?>
    </div>
    <div class="row">
    	<?php query_posts('posts_per_page=2&ignore_sticky_posts=1&cat=-55'); ?>
        <?php if(have_posts()): while(have_posts()): the_post(); ?>
    	<?php get_template_part('content','main'); ?>
        <?php endwhile; endif; ?>
    </div>
    <div class="row">
    	<?php query_posts('posts_per_page=8&offset=2&ignore_sticky_posts=1&cat=-55'); ?>
        <?php if(have_posts()): while(have_posts()): the_post(); ?>
    	<?php get_template_part('content','newslist'); ?>
        <?php endwhile; endif; ?>
    </div>

    どなたかご教授いただけないでしょうか。
    設定などに問題があるのでしょうか。

7件の返信を表示中 - 1 - 7件目 (全7件中)
  • こんにちは

    endwhileの後に

    wp_reset_query();

    を追加してみてください

    トピック投稿者 regulus-design

    (@regulus-design)

    ご指導ありがとうございます。

    <?php endwhile; ?><?php wp_reset_query(); ?><?php endif; ?>

    こういうことでしょうか?
    また、endwhileが3カ所ありますが、3カ所全てに記入するのでしょうか。
    大変恐縮ですが、教えていただけますれば幸いです。

    http://wpdocs.sourceforge.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wp_reset_query

    などを、参考にして、十分納得できたら、作業してください

    モデレーター jim912

    (@jim912)

    最新記事の ignore_sticky_posts を指定しなければいいのではないですか?

    トピック投稿者 regulus-design

    (@regulus-design)

    お二人ともご指導ありがとうございます。
    ignore_sticky_posts をはずしたところ、
    大見出し:最新記事2件
    小見出し:最新記事3〜8件
    の両方の1番上に先頭に指定した記事が来てしまいました。

    モデレーター jim912

    (@jim912)

    では、最新記事の指定に get_option(‘sticky_posts’) を、post__not_in で指定されてはいかがでしょう。

    トピック投稿者 regulus-design

    (@regulus-design)

    時間が掛かってしまいましたが、
    アドバイスを基に解決いたしました。

    <div class="row">
      <?php
        $myquery_pickup = array(
    	'posts_per_page' => 1,
    	'ignore_sticky_posts' => 1,
    	'post__in' => get_option('sticky_posts')
      ); ?>
        <?php query_posts($myquery_pickup); ?>
        <?php if(have_posts()): while(have_posts()): the_post(); ?>
    	<?php get_template_part('content','pickup'); ?>
        <?php endwhile; endif; ?>
    </div>
    <div class="row">
    <?php $myquery_recent01 = array(
    	'posts_per_page' => 2,
    	'ignore_sticky_posts' => 1,
    	'post__not_in' => get_option('sticky_posts'),
    	'tax_query' => array(array(
    		'taxonomy' => 'post_format',
    		'field' => 'slug',
    		'terms' => 'post-format-status',
    		'operator' => 'NOT IN'
    	))
    ); ?>
    <?php query_posts($myquery_recent01); ?>
        <?php if(have_posts()): while(have_posts()): the_post(); ?>
    	<?php get_template_part('content','main'); ?>
        <?php endwhile; endif; ?>
    </div>
    <div class="row">
    <?php $myquery_recent02 = array(
    	'posts_per_page' => 8,
    	'offset' => 2,
    	'ignore_sticky_posts' => 1,
    	'post__not_in' => get_option('sticky_posts'),
    	'tax_query' => array(array(
    		'taxonomy' => 'post_format',
    		'field' => 'slug',
    		'terms' => 'post-format-status',
    		'operator' => 'NOT IN'
    	))
    ); ?>
    <?php query_posts($myquery_recent02 . '&cat=-55'); ?>
        <?php if(have_posts()): while(have_posts()): the_post(); ?>
    	<?php get_template_part('content','newslist'); ?>
        <?php endwhile; endif; ?>
    </div>

    あまり綺麗なコードではないかもしれませんが、
    思うように動き、大変助かりました。どうもありがとうございました。

7件の返信を表示中 - 1 - 7件目 (全7件中)
  • トピック「投稿を先頭に固定表示すると下の記事一覧にも同じ記事が表示されてしま」には新たに返信することはできません。