2

我为我的 Wordpress 平台创建了一个小部件,用于显示本周最热门的帖子。但是,它有一个问题。它计算的是星期一最流行的帖子,而不是过去 7 天。例如,这意味着在星期二,它将只包括星期二和星期一的帖子。

这是我的小部件代码:

<?php class PopularWidget extends WP_Widget
{
    function PopularWidget(){
        $widget_ops = array('description' => 'Displays Popular Posts');
        $control_ops = array('width' => 400, 'height' => 300);
        parent::WP_Widget(false,$name='ET Popular Widget',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? 'Popular This Week' : $instance['title']);
        $postsNum = empty($instance['postsNum']) ? '' : $instance['postsNum'];
        $show_thisweek = isset($instance['thisweek']) ? (bool) $instance['thisweek'] : false;

        echo $before_widget;

        if ( $title )
        echo $before_title . $title . $after_title;

?>
<?php
    $additional_query = $show_thisweek ? '&year=' . date('Y') . '&w=' . date('W') : '';

    query_posts( 'post_type=post&posts_per_page='.$postsNum.'&orderby=comment_count&order=DESC' . $additional_query ); ?>
        <div class="widget-aligned">
        <h3 class="box-title">Popular Articles</h3>
        <div class="blog-entry">
            <ol>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <li><h4 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4></li>
    <?php endwhile; endif; wp_reset_query(); ?>
            </ol>
        </div>
        </div> <!-- end widget-aligned -->
        <div style="clear:both;"></div>
<?php
        echo $after_widget;
    }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['title'] = stripslashes($new_instance['title']);
        $instance['postsNum'] = stripslashes($new_instance['postsNum']);
        $instance['thisweek'] = 0;
        if ( isset($new_instance['thisweek']) ) $instance['thisweek'] = 1;

        return $instance;
    }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
        //Defaults
        $instance = wp_parse_args( (array) $instance, array('title'=>'Popular Posts', 'postsNum'=>'','thisweek'=>false) );

        $title = htmlspecialchars($instance['title']);
        $postsNum = htmlspecialchars($instance['postsNum']);


        # Title
        echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title:' . '</label><input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></p>';
        # Number of posts
        echo '<p><label for="' . $this->get_field_id('postsNum') . '">' . 'Number of posts:' . '</label><input class="widefat" id="' . $this->get_field_id('postsNum') . '" name="' . $this->get_field_name('postsNum') . '" type="text" value="' . $postsNum . '" /></p>';  ?>
        <input class="checkbox" type="checkbox" <?php checked($instance['thisweek'], 1) ?> id="<?php echo $this->get_field_id('thisweek'); ?>" name="<?php echo $this->get_field_name('thisweek'); ?>" />
        <label for="<?php echo $this->get_field_id('thisweek'); ?>"><?php esc_html_e('Popular this week','Aggregate'); ?></label>
        <?php
    }

}// end AboutMeWidget class

function PopularWidgetInit() {
  register_widget('PopularWidget');
}

add_action('widgets_init', 'PopularWidgetInit');

?>

如何更改此脚本,以便计算过去 7 天而不是上周一的帖子?

4

1 回答 1

2

好的,根据您的请求和 Diego 的链接,这样的东西(未经测试)应该可以工作。

首先,将一个函数添加到您的类中,该函数将过滤由 选择的行query_posts

function this_week_filter($where = '') {
    $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-7 days')) . "'";
    return $where;
}

其次,在调用之前添加过滤器(如果需要)query_posts

if ($show_thisweek) {
    add_filter('posts_where', array($this, 'this_week_filter'));
}

为了安全起见,请在调用后将其删除query_posts(因此它不会影响您运行的任何其他查询):

if ($show_thisweek) {
    remove_filter('posts_where', array($this, 'this_week_filter'));
}

把它们放在一起:

<?php class PopularWidget extends WP_Widget
{
    function PopularWidget(){
        $widget_ops = array('description' => 'Displays Popular Posts');
        $control_ops = array('width' => 400, 'height' => 300);
        parent::WP_Widget(false,$name='ET Popular Widget',$widget_ops,$control_ops);
    }

    function this_week_filter($where = '') {
        $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-7 days')) . "'";
        return $where;
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? 'Popular This Week' : $instance['title']);
        $postsNum = empty($instance['postsNum']) ? '' : $instance['postsNum'];
        $show_thisweek = isset($instance['thisweek']) ? (bool) $instance['thisweek'] : false;

        echo $before_widget;

        if ( $title )
        echo $before_title . $title . $after_title;

        if ($show_thisweek) {
            add_filter('posts_where', array($this, 'this_week_filter'));
        }


        query_posts( 'post_type=post&posts_per_page='.$postsNum.'&orderby=comment_count&order=DESC'); ?>
        <div class="widget-aligned">
        <h3 class="box-title">Popular Articles</h3>
        <div class="blog-entry">
            <ol>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <li><h4 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4></li>
            <?php endwhile; endif;

            if ($show_thisweek) {
                remove_filter('posts_where', array($this, 'this_week_filter'));
            }

            wp_reset_query(); ?>
            </ol>
        </div>
        </div> <!-- end widget-aligned -->
        <div style="clear:both;"></div>
<?php
        echo $after_widget;
    }

其余代码不变。

可能可以remove_filter提前(直接在 之后)移动呼叫query_posts,但我现在无法测试。

于 2012-12-11T17:01:40.170 回答