我看到了很多解决方案,但是它们不起作用... 我的wordpress主题是Schema Lite!
//为了显示帖子浏览量
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 issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
//
get_header(); ?>
<div id="page" class="single clear">
<div class="content">
<article class="article">
<?php
// Elementor `single` location.
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'single' ) ) {
if ( have_posts() ) :
while ( have_posts() ) :
setPostViews(get_the_ID());
the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class( 'post' ); ?>>
<div class="single_post">
<?php if ( '1' === $schema_lite_single_breadcrumb_section && empty( $disable_title ) ) { ?>
<div class="breadcrumb" xmlns:v="http://rdf.data-vocabulary.org/#"><?php schema_lite_the_breadcrumb(); ?></div>
<?php } ?>
<?php if ( empty( $disable_title ) || empty( $disable_post_meta ) ) { ?>
<header>
<?php if ( empty( $disable_title ) ) { ?>
<h1 class="title single-title"><?php the_title(); ?></h1>
<?php } ?>
<?php if ( empty( $disable_post_meta ) ) { ?>
<div class="post-info">
<!-- <span class="theauthor"><i class="schema-lite-icon icon-user"></i> <?php esc_html_e( 'By', 'schema-lite' ); ?> <?php the_author_posts_link(); ?></span> -->
<span class="posted-on entry-date date updated"><i class="schema-lite-icon icon-calendar"></i> <?php the_time( get_option( 'date_format' ) ); ?></span>
<span class="featured-cat"><i class="schema-lite-icon icon-tags"></i> <?php the_category( ', ' ); ?></span>
<span> <?php echo getPostViews(get_the_ID()); ?> </span>
我很久以来一直试图解决此问题
但是,无论我如何更改 single.php 中的 getPostViews 和 setPostViews 函数的位置,它都不起作用。< / p>
顺便说一下,这是我的网站的网址:enter link description here
希望有人可以帮助我! 预先感谢!
答案 0 :(得分:0)
交换setPostViews(get_the_ID());
和the_post();
的顺序
如果您在the_post之前调用get_the_ID()
,则您返回的调用将返回不可靠的结果,因为the_post
将在循环中设置您要查看的帖子,并允许所有标准WordPress模板函数返回正确的信息。
get_header(); ?>
<div id="page" class="single clear">
<div class="content">
<article class="article">
<?php
// Elementor `single` location.
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'single' ) ) {
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
setPostViews(get_the_ID());
?>
<div id="post-<?php the_ID(); ?>" <?php post_class( 'post' ); ?>>
<div class="single_post">
<?php if ( '1' === $schema_lite_single_breadcrumb_section && empty( $disable_title ) ) { ?>
<div class="breadcrumb" xmlns:v="http://rdf.data-vocabulary.org/#"><?php schema_lite_the_breadcrumb(); ?></div>
<?php } ?>
<?php if ( empty( $disable_title ) || empty( $disable_post_meta ) ) { ?>
<header>
<?php if ( empty( $disable_title ) ) { ?>
<h1 class="title single-title"><?php the_title(); ?></h1>
<?php } ?>
<?php if ( empty( $disable_post_meta ) ) { ?>
<div class="post-info">
<!-- <span class="theauthor"><i class="schema-lite-icon icon-user"></i> <?php esc_html_e( 'By', 'schema-lite' ); ?> <?php the_author_posts_link(); ?></span> -->
<span class="posted-on entry-date date updated"><i class="schema-lite-icon icon-calendar"></i> <?php the_time( get_option( 'date_format' ) ); ?></span>
<span class="featured-cat"><i class="schema-lite-icon icon-tags"></i> <?php the_category( ', ' ); ?></span>
<span> <?php echo getPostViews(get_the_ID()); ?> </span>