我试图展示前5位最受欢迎(浏览量最大)的博客。它正常工作,然后我在functions.php
在我构建此文件时显示视图,以便查看其工作情况。
问题是,当我点击一篇文章时,它们在视图中都是+1,即使它们按照应该的顺序将我的浏览次数从最多到最少排序,它们仍然相互连接。如何让他们独立地访问++而不连接?
功能。php
/* most popular script to count the posts */
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\';
}
/* the script to display the view count */
function bac_PostViews($postID) {
//Set the name of the Posts Custom Field.
$count_key = \'post_views_count\';
//Returns values of the custom field with the specified key from the specified post.
$count = get_post_meta($postID, $count_key, true);
//If the the Post Custom Field value is empty.
if($count == \'\'){
$count = 0; // set the counter to zero.
//Delete all custom fields with the specified key from the specified post.
delete_post_meta($postID, $count_key);
//Add a custom (meta) field (Name/value)to the specified post.
add_post_meta($postID, $count_key, \'0\');
return $count . \' View\';
//If the the Post Custom Field value is NOT empty.
}else{
$count++; //increment the counter by 1.
//Update the value of an existing meta key (custom field) for the specified post.
update_post_meta($postID, $count_key, $count);
//If statement, is just to have the singular form \'View\' for the value \'1\'
if($count == \'1\'){
return $count . \' View\';
}
//In all other cases return (count) Views
else {
return $count . \' Views\';
}
}
}
回路
<?php $catquery = new WP_Query(array(
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'posts_per_page\' => 5,
\'cat\' => \'52\',
)); ?>
<ol class="topheadlines">
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<!-- display view count -->
<?php if(function_exists(\'bac_PostViews\')) {
echo bac_PostViews(get_the_ID());
}?>
<div class="row">
<div class="col-9" style="padding:0px 0px 0px 5px;">
<li class="topheadlines-text"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
</div>
<div class="col-3 topheadline-thumb" style="padding:0px 5px 0px 5px;">
<?php echo get_the_post_thumbnail( $post->ID, \'large\' ); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
?>
单身。php
<?php
/**
* The template for displaying all single posts
*
* @package understrap
*/
// Exit if accessed directly.
defined( \'ABSPATH\' ) || exit;
get_header();
$container = get_theme_mod( \'understrap_container_type\' );
?>
<div class="row" style="margin:0;">
<div class="col-md-9 single-bg" style="padding:0;">
<div class="container-fluid">
<div class="single-wrap">
<h5 class="single-date">
<?php echo get_the_date(); ?>
</h5>
<h2 class="single-title">
<?php the_title(); ?>
</h2>
<h3 class="single-sub-title">
<?php echo the_field(\'mt_post_subtitle\'); ?>
</h3>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="single-thumb">
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name;
?>
<?php if($firstCategory != \'Daily (Email)\') { ?>
<?php echo get_the_post_thumbnail( $post->ID, \'large\' );
}
?>
</div>
<div class="single-content">
<?php the_content(); ?>
</div>
<?php
$author_id = get_the_author_meta(\'ID\');
$author_badge = get_field(\'author_badge\', \'user_\'. $author_id );
?>
<div class="author-wrap clearfix">
<div class="author-pic">
<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>">
<?php echo get_avatar( get_the_author_meta( \'ID\' ) , 50 ); ?>
</a>
</div>
<h5 class="author-name">
<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>">
<?php the_author(); ?><br><span class="author-title"><?php the_field(\'title\', \'user_\'. $author_id ); ?>, The Hustle</span>
</a>
</h5>
</div><!-- end author wrap -->
<?php endwhile; ?>
<?php endif; ?>
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name;
?>
<?php if($firstCategory == \'Daily (Email)\') { ?>
<a href="/daily">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Stories\') { ?>
<a href="/stories">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Blog\') { ?>
<a href="/blog">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Episodes\') { ?>
<a href="/">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'\') { ?>
<a href="/">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Uncategorized\') { ?>
<a href="/">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Brief\') { ?>
<a href="/series/brief">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<div class="recent-wrap">
<h4 class="featured-headlines">
Recent Posts
</h4>
<?php echo do_shortcode(\'[recent_post_carousel design="design-1" category="52,6"]\'); ?>
</div><!-- end recent wrap -->
</div><!-- end singlewrap -->
</div><!-- end container fluid -->
</div><!-- end col 9 -->
<div class="col-md-3" style="padding:0; margin:0;">
<?php include get_template_directory() . \'/inc/page-sidebar.php\'; ?>
</div><!-- end col 3 sidebar -->
</div> <!-- end master row -->
<?php get_footer();
最合适的回答,由SO网友:Amir 整理而成
在循环中使用此选项:
getPostViews(get_the_ID());
而不是:
bac_PostViews(get_the_ID());
所以你的循环应该是这样的:
<?php $catquery = new WP_Query(array(
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'posts_per_page\' => 5,
\'cat\' => \'52\',
)); ?>
<ol class="topheadlines">
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<!-- display view count -->
<?php if(function_exists(\'getPostViews\')) {
echo getPostViews(get_the_ID());
}?>
<div class="row">
<div class="col-9" style="padding:0px 0px 0px 5px;">
<li class="topheadlines-text"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
</div>
<div class="col-3 topheadline-thumb" style="padding:0px 5px 0px 5px;">
<?php echo get_the_post_thumbnail( $post->ID, \'large\' ); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
?>
而且是单曲。php您必须调用bac\\u PostViews函数:
<?php
/**
* The template for displaying all single posts
*
* @package understrap
*/
// Exit if accessed directly.
defined( \'ABSPATH\' ) || exit;
get_header();
$container = get_theme_mod( \'understrap_container_type\' );
?>
<div class="row" style="margin:0;">
<div class="col-md-9 single-bg" style="padding:0;">
<div class="container-fluid">
<div class="single-wrap">
<h5 class="single-date">
<?php echo get_the_date(); ?>
</h5>
<h2 class="single-title">
<?php the_title(); ?>
</h2>
<h3 class="single-sub-title">
<?php echo the_field(\'mt_post_subtitle\'); ?>
</h3>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="single-thumb">
<?php if(function_exists(\'bac_PostViews\')) {
echo bac_PostViews(get_the_ID());
}?>
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name;
?>
<?php if($firstCategory != \'Daily (Email)\') { ?>
<?php echo get_the_post_thumbnail( $post->ID, \'large\' );
}
?>
</div>
<div class="single-content">
<?php the_content(); ?>
</div>
<?php
$author_id = get_the_author_meta(\'ID\');
$author_badge = get_field(\'author_badge\', \'user_\'. $author_id );
?>
<div class="author-wrap clearfix">
<div class="author-pic">
<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>">
<?php echo get_avatar( get_the_author_meta( \'ID\' ) , 50 ); ?>
</a>
</div>
<h5 class="author-name">
<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>">
<?php the_author(); ?><br><span class="author-title"><?php the_field(\'title\', \'user_\'. $author_id ); ?>, The Hustle</span>
</a>
</h5>
</div><!-- end author wrap -->
<?php endwhile; ?>
<?php endif; ?>
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name;
?>
<?php if($firstCategory == \'Daily (Email)\') { ?>
<a href="/daily">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Stories\') { ?>
<a href="/stories">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Blog\') { ?>
<a href="/blog">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Episodes\') { ?>
<a href="/">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'\') { ?>
<a href="/">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Uncategorized\') { ?>
<a href="/">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<?php if($firstCategory == \'Brief\') { ?>
<a href="/series/brief">
<button type="button" class="btn btn-primary single-read-more"><?php echo $button_text; ?>Related Posts</button>
</a>
<?php
}
?>
<div class="recent-wrap">
<h4 class="featured-headlines">
Recent Posts
</h4>
<?php echo do_shortcode(\'[recent_post_carousel design="design-1" category="52,6"]\'); ?>
</div><!-- end recent wrap -->
</div><!-- end singlewrap -->
</div><!-- end container fluid -->
</div><!-- end col 9 -->
<div class="col-md-3" style="padding:0; margin:0;">
<?php include get_template_directory() . \'/inc/page-sidebar.php\'; ?>
</div><!-- end col 3 sidebar -->
</div> <!-- end master row -->
<?php get_footer();