按类别显示最热门的帖子

时间:2014-08-10 11:44:39

标签: php

我有一个小部件,首先显示视图数量最多的帖子。但是,它实际上显示了所有类别的帖子。我想只显示特定类别中最热门的帖子,我该怎么做?

以下是代码:

 <?php

    class anthemes_hotposts extends WP_Widget {
         function anthemes_hotposts() {
          $widget_ops = array('description' => 'Hot Posts by Views' );
            parent::WP_Widget(false, $name = 'Custom: Hot Posts by Views',$widget_ops); 
        }


        function widget($args, $instance) {   
            extract( $args );
        $number = $instance['number'];
            $title = $instance['title'];
            ?>



    <?php echo $before_widget; ?>
    <?php if ( $title ) echo $before_title . $title . $after_title; ?>

    <ul class="article_list">
    <?php $inntop = new WP_Query(array('meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => $number )); ?>
    <?php while ($inntop->have_posts()) : $inntop->the_post(); ?>  

      <li><a href="<?php the_permalink(); ?>">
          <?php if ( has_post_thumbnail()) { ?>
          <?php echo the_post_thumbnail('thumbnail-widget'); ?>
          <?php } else { ?><img src="http://placehold.it/80x80&amp;text=No+Image" width="80" height="80" alt="article image" /><?php } ?></a>

          <div class="an-display-time"><?php echo time_ago(); ?> <?php _e('ago', 'anthemes'); ?></div><br />
          <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
          <div class="sub-article-widget">
            <div class="an-display-view"><i class="fa fa-eye"></i> <?php echo getPostViews(get_the_ID()); ?></div>
            <div class="an-display-like"><?php if( function_exists('zilla_likes') ) zilla_likes(); ?></div>
            <div class="an-display-comm"><i class="fa fa-comments"></i> <?php comments_popup_link('0', '1', '%'); ?></div><div class="clear"></div>        
          </div>
      </li>

    <?php endwhile; wp_reset_query(); ?>
    </ul>


    <?php echo $after_widget; ?> 


    <?php
        }
        function update($new_instance, $old_instance) {       
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = strip_tags($new_instance['number']);
        return $instance;
        }

      function form( $instance ) {
        $instance = wp_parse_args( (array) $instance );
    ?>


            <p>
              <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label> 
              <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php if( isset($instance['title']) ) echo $instance['title']; ?>" />
            </p>

             <p>
              <label for="<?php echo $this->get_field_id('number'); ?>">Number of Posts:</label>      
              <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php if( isset($instance['number']) ) echo $instance['number']; ?>" />
             </p> 

    <?php  } } 
    add_action('widgets_init', create_function('', 'return register_widget("anthemes_hotposts");')); // register widget
    ?>

0 个答案:

没有答案