我环顾四周,很遗憾,我无法找到任何可以帮助我对仪表板小部件中查看次数最多的帖子进行排序的内容。我能够显示帖子以及他们被查看了多少次,但看起来它是按最近的帖子拉动和排序而不是查看的时间。
以下是代码。任何人都可以帮助我吗?
// Adds view counter
function getCoupontViews($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 "Used 0 Times";
}
return 'Used ' .$count. ' Times';
}
// Displays the view counter
function setCouponViews($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);
}
}
function clipit_views_db() {
?>
<ol>
<?php
global $post;
$args = array(
'numberposts' => 5,
'post_type' => 'coupon'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php echo getCoupontViews(get_the_ID()); ?> </li>
<?php endforeach; ?>
</ol>
<?php
}
function add_clipit_views_db() {
wp_add_dashboard_widget( 'clipit_views_db', __( 'Recent ClipIt Views' ), 'clipit_views_db' );
}
add_action('wp_dashboard_setup', 'add_clipit_views_db' );
答案 0 :(得分:0)
看起来我可以通过向get_posts()
添加其他信息来解决这个问题function clipit_views_db() {
?>
<ol>
<?php
global $post;
$args = array(
'meta_key' => 'post_views_count',
'numberposts' => 5,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => 'coupon'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php echo getCoupontViews(get_the_ID()); ?> </li>
<?php endforeach; ?>
</ol>
<?php
}
function add_clipit_views_db() {
wp_add_dashboard_widget( 'clipit_views_db', __( 'Recent ClipIt Views' ), 'clipit_views_db' );
}
add_action('wp_dashboard_setup', 'add_clipit_views_db' );