Popular Post Not Show

时间:2016-11-27 作者:Hasral Ngt

我在函数和插件小部件中有代码,请参见下面的代码。

function.php //显示计数

function hwd_post_views(){
$post_id = get_the_ID();
$count_key = \'post_views_count\';
$n = get_post_meta($post_id, $count_key, true);
if ($n > 999999999) {
    $n_format = number_format($n / 1000000000, 1) . \'B\';
} else if ($n > 999999) {
    $n_format = number_format($n / 1000000, 1) . \'M\';
} else if ($n > 999) {
        $n_format = number_format($n / 1000, 1) . \'K\';
} else {
    $n_format = $n;
} echo $n_format;}
Widget-pop.php //插件显示流行帖子

/**
 * Plugin Name: Popular Posts Widget
 */

add_action( \'widgets_init\', \'hwd_pop_load_widgets\' );

function hwd_pop_load_widgets() {
    register_widget( \'hwd_pop_widget\' );
}

class hwd_pop_widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function hwd_pop_widget() {
        /* Widget settings. */
        $widget_ops = array( \'classname\' => \'hwd_pop_widget\', \'description\' => __(\'A widget that displays a list of popular posts within a time period of your choice.\', \'hwd-text\') );

        /* Widget control settings. */
        $control_ops = array( \'width\' => 250, \'height\' => 300, \'id_base\' => \'hwd_pop_widget\' );

        /* Create the widget. */
        $this->__construct( \'hwd_pop_widget\', __(\'HWD: Popular Posts Widget\', \'hwd-text\'), $widget_ops, $control_ops );
    }

    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );

        /* Our variables from the widget settings. */
        global $post;
        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $popular_days = $instance[\'popular_days\'];
        $number = $instance[\'number\'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Display the widget title if one was input (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title;

        ?>

        <div id="terpopuler" class="terpopuler__row">
            <ul class="terpopuler__wrap">
                <?php $i = 0; $popular_days_ago = \'$popular_days days ago\'; $recent = new WP_Query(array( \'posts_per_page\' => $number, \'orderby\' => \'meta_value_num\', \'order\' => \'DESC\', \'meta_key\' => \'post_views_count\', \'date_query\' => array( array( \'after\' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>
                <li class="terpopuler__item">
                    <a href="<?php the_permalink(); ?>" rel="bookmark">
                        <div class="terpopuler__num"><?php $i++; echo $i ?></div>
                        <div class="terpopuler__title">
                            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                        </div>
                        <?php $post_views = get_post_meta($post->ID, \'post_views_count\', true); if ( $post_views >= 1) { ?>
                            <span class="terpopuler__info"><?php hwd_post_views(); ?> kali dibaca</span>
                        <?php } ?>
                    </a>
                </li>
                <?php endwhile; ?>
            </ul>
        </div><!--widget-terpopuler-->

        <?php

        /* After widget (defined by themes). */
        echo $after_widget;
    }

    /**
     * Update the widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        /* Strip tags for title and name to remove HTML (important for text inputs). */
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'popular_days\'] = strip_tags( $new_instance[\'popular_days\'] );
        $instance[\'number\'] = strip_tags( $new_instance[\'number\'] );

        return $instance;
    }


    function form( $instance ) {

        /* Set up some default widget settings. */
        $defaults = array( \'title\' => \'Title\', \'number\' => 5, \'popular_days\' => 30 );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>">Title:</label>
            <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:90%;" />
        </p>

        <!-- Number of days -->
        <p>
            <label for="<?php echo $this->get_field_id( \'popular_days\' ); ?>">Number of days to use for Trending topics:</label>
            <input id="<?php echo $this->get_field_id( \'popular_days\' ); ?>" name="<?php echo $this->get_field_name( \'popular_days\' ); ?>" value="<?php echo $instance[\'popular_days\']; ?>" size="3" />
        </p>

        <!-- Number of posts -->
        <p>
            <label for="<?php echo $this->get_field_id( \'number\' ); ?>">Number of posts to display:</label>
            <input id="<?php echo $this->get_field_id( \'number\' ); ?>" name="<?php echo $this->get_field_name( \'number\' ); ?>" value="<?php echo $instance[\'number\']; ?>" size="3" />
        </p>


    <?php
    }
}
请参见图片

here

我不明白,怎么修?

UPDATE

我的问题没有答案。我包括文件widget-pop.php 到文件function.php 在我的主题孩子。

如果使用localhost(Xampp),则可以正常工作。请访问我的网站radarsulselcom

谢谢

2 个回复
SO网友:Vicetías

试试这个$i = 0;

<?php $popular_days_ago = \'$popular_days days ago\'; $recent = new WP_Query(array( \'posts_per_page\' => $number, \'orderby\' => \'meta_value_num\', \'order\' => \'DESC\', \'meta_key\' => \'post_views_count\', \'date_query\' => array( array( \'after\' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>

SO网友:Vicetías

我有一个问题:$popular_days_ago = \'$popular_days days ago\'

这显示了几天前发布的热门帖子,但有人知道如何显示过去24小时内查看的最热门帖子,无论多少天前发布的帖子是这样的popular posts plugin? 我是说,如果一篇文章被发表a month ago 但在last day 这是过去24小时内浏览量最大的一次,应该出现在流行/趋势帖子中,但是$popular_days_ago 仅显示在所述时间内发布的帖子。