[英]popular posts for custom post type wordpress
我想顯示自定義帖子類型的流行帖子,而我的代碼僅適用於帖子。 不適用於自定義帖子類型。 這是我的代碼。 在 function.php 我正在使用這個
function wpb_set_post_views($postID) {
$count_key = 'wpb_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);
在自定義帖子類型的單頁中,我在循環中使用了它
wpb_set_post_views(get_the_ID());
在我想顯示自定義帖子類型的熱門帖子的主頁中,我使用了這個
<?php
$args = array(
'post_type' => 'project',
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$query = new WP_Query( $args );
while ($query->have_posts()): $query->the_post();
the_title();
endwhile
?>
我會感謝你的好意
我認為您的代碼缺少一些有用的東西,所以如果您覺得可以,我會稍微調整您的代碼。
您可能需要一個 function 來顯示視圖
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";
}
return $count;
}
在您的 function 中計算查看次數,您可以設置一個 cookie,以便僅計算不是同一用戶的查看次數。
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{
if(!isset($_COOKIE['wp_visited_ip_'.$postID])) { // check if cookie exist
$count++; // increase counter
}
update_post_meta($postID, $count_key, $count);
// Get IP address
$visit_ip_addr = $_SERVER['REMOTE_ADDR'];
// Set the cookie
setcookie('wp_visited_ip_'.$postID, $visit_ip_addr, time()+ (60 * 1)); // time interval
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
在您帖子的 single.php 中,您可以在循環中調用 function。
setPostViews(get_the_ID()); // count post views
顯示查看次數最多的 5 個帖子的查詢可能如下所示:
$args_fav = array(
'post_type' => 'project',
'order' => 'DESC',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'post_status' => array( 'publish' ),
'posts_per_page' => 5
);
$loop_fav = new WP_Query( $args_fav );
這段代碼應該可以正常工作。
我們需要做的第一件事是創建一個 function,它將檢測帖子的瀏覽次數並將其存儲為每個帖子的自定義字段。
function wpb_set_post_views($postID) {
$count_key = 'wpb_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);
}}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
在您帖子的 single.php 中,您可以在循環內調用 function。
wpb_set_post_views(get_the_ID());
如果要按查看次數對帖子進行排序,則可以使用 wp_query post_meta 參數輕松完成。 最基本的示例循環查詢如下所示:
<?php
$popularpost = new WP_Query( array(
'post_type' => ' ',// add the name for your post type
'posts_per_page' => 4,
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC' ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>
我希望這可以幫助你歡呼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.