wordpressの質問です。
タイトル通り、「プラグインなしで月別カスタム投稿記事ランキング表示」したいです。
総合カスタム投稿記事ランキングは表示することができているので、そのコードと月間PV数取得のコードを足したらできると思っていたのですが、そんなうまいこといくわけがありませんでした。
③のコードで月間PV数を取得しているので、あとは①のコードに何かを足したらいいと思っていたのですが...
そこの答えがなかなかわからないです。
以下ののコードを表示したいところに足したのですが、2行目で「syntax error, unexpected ','」というエラーが出ました。
「,」は、足りているような...
PHP
1<?php 2 'example', 3 'numberposts' => 5, 4 'meta_key' => 'pv_count_week', 5 'orderby' => 'meta_value_num', 6 'order' => 'DESC', 7); 8$posts = get_posts( $args ); 9if( $posts ): ?>
PHPについて知識がないので、苦戦中です。
ご教授、宜しくお願い致します。
①総合記事ランキング表示部分
PHP
1<?php 2 $args = array( 3 'post_type' => 'product_samples' 4 ); ?> 5 6 <?php $my_query = new WP_Query( $args ); ?> 7 <ul class="boxWrap clearfix"> 8 <?php 9 $popular = new WP_Query(array( 10 'posts_per_page' => 5, 11 'meta_key' => 'post_views_count',//投稿数をカウントするカスタムフィールド名 12 'orderby' => 'meta_value_num', 13 'order' => 'DESC', 14 'post_type' => array('example'), 15 'post_status' => 'publish', 16 'caller_get_posts' => 1, 17 'offset' => 0, 18 )); 19 while ($popular->have_posts()) : $popular->the_post(); 20 ?> 21 <!-- サムネイルの表示 --> 22 <li class="block circle"> 23 <a href="<?php echo get_permalink(); ?>"> 24 <div class="mosaic-backdrop"> 25 <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'post-thumbnail'); } ?> 26 <div class="info topinfo"> 27 <p> 28 <?php // 連番表示 29 $count = sprintf("%02d",$count); // 一桁を二桁に 30 echo $count + 1; // 01を出力 31 $count++; 32 ?> 33 </p> 34 <div class="modelName"> 35 <span class="name"> 36 <?php the_title(); ?> 37 <span id="likeCount3"></span> 38 </span> 39 <span class="univ"><?php echo get_secondary_title(); ?></span> 40 </div> 41 </div> 42 </div> 43 </a> 44 </li> 45 <?php endwhile; ?> 46 </ul> 47 <?php wp_reset_postdata(); ?>
②総合記事ランキング(functions.php)
PHP
1// 人気記事総合 2function getPostViews($postID){ 3 $count_key = 'post_views_count'; 4 $count = get_post_meta($postID, $count_key, true); 5 if($count==''){ 6 delete_post_meta($postID, $count_key); 7 add_post_meta($postID, $count_key, '0'); 8 return "0 View"; 9 } 10 return $count.' Views'; 11} 12function setPostViews($postID) { 13 $count_key = 'post_views_count'; 14 $count = get_post_meta($postID, $count_key, true); 15 if($count==''){ 16 $count = 0; 17 delete_post_meta($postID, $count_key); 18 add_post_meta($postID, $count_key, '0'); 19 }else{ 20 $count++; 21 update_post_meta($postID, $count_key, $count); 22 } 23} 24remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
③月間PV数取得(functions.php)
PHP
1//アクセス数をカウントする 2function set_post_views_month() { 3 $postID = get_the_ID(); 4 $num = (int)date_i18n('H'); // 現在時間で番号取得 5 $key = 'pv_count_month'; 6 $count_key = '_pv_count_month'; 7 $count_array = get_post_meta( $postID, $count_key, true ); 8 $sum_count = get_post_meta( $postID, $key, true ); 9 if( !is_array($count_array) ) { //配列ではない 10 $count_array = array(); 11 $count_array[$num] = 1; 12 } else { //配列である 13 if ( isset( $count_array[$num] ) ) { //カウント配列[n]が存在する 14 $count_array[$num] += 1; 15 } else { //カウント配列[n]が存在しない 16 $count_array[$num] = 1; 17 } 18 } 19 //アクセス数を更新する 20 update_post_meta( $postID, $count_key, $count_array ); 21 update_post_meta( $postID, $key, $sum_count + 1 ); 22} 23 24//アクセス数をリセットする 25function reset_post_views_month() { 26 $num = (int)date_i18n('H'); 27 $key = 'pv_count_month'; 28 $reset_key = '_pv_count_month'; 29 $args = array( 30 'posts_per_page' => -1, 31 'post_type' => 'your_post_name', 32 'post_status'=>'publish', 33 'meta_key' => $reset_key, 34 ); 35 $reset_posts = get_posts($args); 36 if($reset_posts): 37 foreach($reset_posts as $reset_post): 38 $postID = $reset_post->ID; 39 $count_array = get_post_meta( $postID , $reset_key, true ); 40 if ( isset( $count_array[$num] ) ) { //カウント配列[n]が存在する 41 $count_array[$num] = 0; 42 } 43 //アクセス数をリセットする 44 update_post_meta( $postID, $reset_key, $count_array ); 45 update_post_meta( $postID, $key, array_sum( $count_array ) ); 46 endforeach; 47 endif; 48} 49 50//リセット関数を実行するアクションフックを追加 51// add_action( 'set_hours_event', 'reset_post_views' ); 52add_action( 'set_week_event', 'reset_post_views_week' ); 53add_action( 'set_month_event', 'reset_post_views_month' ); 54 55//実行間隔の追加 56function my_interval_week( $schedules ) { 57 // 1週間ごとを追加 58 $schedules['1week'] = array( 59 'interval' => 604800, 60 'display' => 'every 1 week' 61); 62 return $schedules; 63} 64add_filter( 'cron_schedules', 'my_interval_week' ); 65 66//実行間隔の追加 67function my_interval_month( $schedules_month ) { 68 // 1ヶ月ごとを追加 69 $schedules_month['1month'] = array( 70 'interval' => 2592000, 71 'display' => 'every 1 month' 72 ); 73 return $schedules_month; 74} 75add_filter( 'cron_schedules', 'my_interval_month' ); 76 77//アクションフックを定期的に実行するスケジュールイベントの追加 78function my_activation_week() { 79 if ( ! wp_next_scheduled( 'set_week_event' ) ) { 80 wp_schedule_event( 1451574000, '1week', 'set_week_event' ); 81 } 82} 83add_action('wp', 'my_activation_week'); 84 85//アクションフックを定期的に実行するスケジュールイベントの追加 86function my_activation_month() { 87 if ( ! wp_next_scheduled( 'set_month_event' ) ) { 88 wp_schedule_event( 1451574000, '1month', 'set_month_event' ); 89 } 90} 91add_action('wp', 'my_activation_month'); 92 93//ボットの判別 94function isBot() { 95 $bot_list = array ( 96 'Googlebot', 97 'Yahoo! Slurp', 98 'Mediapartners-Google', 99 'msnbot', 100 'bingbot', 101 'MJ12bot', 102 'Ezooms', 103 'pirst; MSIE 8.0;', 104 'Google Web Preview', 105 'ia_archiver', 106 'Sogou web spider', 107 'Googlebot-Mobile', 108 'AhrefsBot', 109 'YandexBot', 110 'Purebot', 111 'Baiduspider', 112 'UnwindFetchor', 113 'TweetmemeBot', 114 'MetaURI', 115 'PaperLiBot', 116 'Showyoubot', 117 'JS-Kit', 118 'PostRank', 119 'Crowsnest', 120 'PycURL', 121 'bitlybot', 122 'Hatena', 123 'facebookexternalhit', 124 'NINJA bot', 125 'YahooCacheSystem', 126 'NHN Corp.', 127 'Steeler', 128 'DoCoMo', 129 ); 130 $is_bot = false; 131 foreach ($bot_list as $bot) { 132 if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) { 133 $is_bot = true; 134 break; 135 } 136 } 137 return $is_bot; 138}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/19 05:08
2020/01/19 06:29
退会済みユーザー
2020/01/19 06:39
2020/01/19 06:41