やりたいこと:
タグで絞った記事一覧の2ページ目以降を上手く表示させたい。
起こっている問題:
query_posts()とget_posts()を使うと
next_posts_link() previous_posts_link()が上手く動作しない。
WordPressをはじめて扱う初心者の者です。
自分でテンプレートを作ってみようとindex.phpを作成しているところなのですが、
投稿記事一覧を取得するループ処理で詰まってしまいました。
投稿記事をtagで絞って一覧表示させようとしているのですが絞った場合に
次の5件や前の5件などを
next_posts_link()
previous_posts_link()
で処理させた場合に上手く動作しません。
リンクのURLは.../page/2とtagで絞らない場合と同様になるのですが、
表示される記事一覧は変わっていません。最新の5件のままです。(tag絞りは効いています。)
tagの絞り方はquery_posts()とget_posts()両方とも試したのですが結果は同じでした。
query_posts()
<?php query_posts('tag=ワードプレス');?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <article> <header> <h1><a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>"> <?php the_title_attribute(); ?></a></h1> <ul class="post_meta"> <li class="time"><?php the_date(); ?></li> <li class="category"><?php the_category(', '); ?></li> </ul> </header> <a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>"> <?php if(has_post_thumbnail()) {the_post_thumbnail(); } ?></a> <?php the_content('続きを読む >'); ?> </article> <?php endwhile; ?> <?php else : ?> <p>記事は見つかりませんでした。</p> <?php endif; ?> <?php previous_posts_link('新しい投稿ページへ'); ?> <?php next_posts_link('古い投稿ページへ'); ?>
get_posts()
<?php $postslist = get_posts('numberposts=5&tag=ワードプレス'); foreach ($postslist as $post) : setup_postdata($post); ?> <article> <header> <h1><a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>"> <?php the_title_attribute(); ?></a></h1> <ul class="post_meta"> <li class="time"><?php the_date(); ?></li> <li class="category"><?php the_category(', '); ?></li> </ul> </header> <a href="<?php the_permalink(); ?>"title="<?php the_title_attribute(); ?>"> <?php if(has_post_thumbnail()) {the_post_thumbnail(); } ?></a> <?php the_content('続きを読む >'); ?> </article> <?php endforeach; ?> <?php previous_posts_link('新しい投稿ページへ'); ?> <?php next_posts_link('古い投稿ページへ'); ?>
query_posts()とget_posts()を使わない場合はURL.../page/2に上手く次の(前の)記事一覧が表示されます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/04 09:11