Количество просмотров в WordPress увеличилось вдвое - PullRequest
0 голосов
/ 08 июля 2019

Я видел много решений, но они не работают ... Моя тема WordPress - Schema Lite!

  1. Это моя функция в function.php

// для отображения количества просмотров сообщений

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);
  1. Это мой сингл. Php

//

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>
  1. Я уверен, что строка "ссылка ref = short icon ..." отсутствует в моем header.php

Я давно пытался решить эту проблему

Однако, как бы я ни изменял положение функций getPostViews и setPostViews в single.php , он не работает.

Кстати, это URL моего сайта: введите описание ссылки здесь

Надеюсь, кто-нибудь может мне помочь! Заранее спасибо!

1 Ответ

0 голосов
/ 08 июля 2019

Поменяйте местами порядок setPostViews(get_the_ID()); и the_post();

Ваш вызов get_the_ID() вернет ненадежные результаты, если вы вызовете его до the_post , так как 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...