前提
wordpressでブログを作っており、トップページには以下の順番でコンテンツを表示させたい
- 人気記事
- 新着記事
使用テーマ:coccon(https://wp-cocoon.com/)
子テーマでカスタマイズ
問題点
人気記事を表示させると、新着記事のほうにも人気記事が表示されてしまう
おそらく、本来1ページ目に表示されるはずの新着記事が人気記事に変わってしまっている
記事の2ページ目以降は正しく新着記事が表示されている
人気記事を表示させるタグを削除して、新着記事のみ表示させると、1ページ目の新着記事も正しく表示される
試したこと
2つの方法で人気記事を表示させた
1つ目
function.php
// 人気記事出力用 function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 View"; } return $count.' Views'; } function setPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
実際に表示させるタグ
<ul class="hero-thunb__lists" id="heroFadelLists"> <?php // views post metaで記事のPV情報を取得する setPostViews(get_the_ID()); // ループ開始 query_posts('meta_key=post_views_count&orderby=meta_value_num&posts_per_page=5&order=DESC'); while(have_posts()) : the_post(); ?> <li class="hero-thunb__item"> <a class="hero-thunb__anchor" href="<?php the_permalink(); ?>" style="background-image: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>)"> <span class="hero-thunb__desc"> <span class="hero-thunb__desc-data"><?php the_time('Y.n.j'); ?></span> <span class="hero-thunb__desc-title"><?php the_title(); ?></span> </span> </a> </li> <?php endwhile; ?> </ul>
2つ目
以下のサイトを参考に人気記事を表示
http://munouya.com/website/my-popular-posts.html
最後に
新着記事はcocconのデフォルトのままで、特に何もいじっていません。
原因がわからず手詰まりです。ご教授のほどお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/01/05 16:34