2013-07-03 67 views
0

我正在使用以下代碼顯示熱門帖子的主題,並且我需要 某人幫助我編輯此代碼以使其顯示上週的熱門帖子或 個月。WordPresspress:本週或本月的最熱門帖子

<?php 
       $args = array(); 
       $args['posts_per_page'] = $number_posts; 
       $args['orderby'] = 'comment_count'; 
       $args['order'] = 'DESC'; 
       $query = new WP_Query($args); 
       while($query->have_posts()) : $query->the_post(); global $post; ?> 
        <li> 
         <a title="<?php the_title();?>" href="<?php the_permalink();?>"> 
          <figure> 
           <?php if(has_post_thumbnail()) { ?> 
            <a href="<?php the_permalink();?>"> 
             <img src="<?php echo aq_resize(wp_get_attachment_url(get_post_thumbnail_id($post->ID)), 42, 42, true); ?>" alt="<?php the_title();?>" /> 
            </a> 
           <?php } else { ?> 
            <a href="<?php the_permalink();?>"> 
             <img src="<?php echo get_template_directory_uri() . '/img/missing_56.png';?>" alt="<?php the_title();?>" /> 
            </a> 
           <?php } ?> 
          </figure> 
          <p> 
           <a href="<?php the_permalink();?>"><?php the_title();?></a> <br /> 
           <span> <?php _e('Le ', 'Voxis'); the_time("F d, Y");?>, <?php comments_popup_link(esc_html__('0 commentaires','Voxis'), esc_html__('1 commentaire','Voxis'), '% '.esc_html__('commentaires','Voxis')); ?> </span> 
          </p> 
         </a> 
        </li> 
       <?php endwhile; wp_reset_postdata(); ?> 
      </ul> 

回答

0

我還沒有測試下面的代碼,但會保證這會適用於你。

<?php 
$weekday=date("d m Y",strtotime('-7days')); 
while($query->have_posts()) : $query->the_post(); global $post; 

$currentdate=date('d m Y',strtotime($post->post_date)); 
if($weekday < $currentdate) { ?> 
    <li> 
     ... 
     body of code in <li> tag 
     ... 
    </li> 
<?php } ?> 
... 
rest of the code 
?> 
+0

您好感謝Answear但仍然迷惑,如果S可能是如何整合這個代碼,請使其更容易對我,給所有的工作代碼,我需要在副本和過去 – Dolomats

0
replace your code to this(get most recent last month post) 
<?php 
       $today = getdate(); 
        $args=array(
          'post_type' => 'post', 
          'year'=>$today["year"], 
          'monthnum'=>$today["mon"]-1, 
          'orderby'=>'comment_count', 
          'order'=>'DESC', 
          'posts_per_page' =>$number_posts 
        ); 

        $query = new WP_Query($args); 
        while($query->have_posts()) : $query->the_post(); global $post; ?> 
         <li> 
          <a title="<?php the_title();?>" href="<?php the_permalink();?>"> 
           <figure> 
            <?php if(has_post_thumbnail()) { ?> 
             <a href="<?php the_permalink();?>"> 
              <img src="<?php echo aq_resize(wp_get_attachment_url(get_post_thumbnail_id($post->ID)), 42, 42, true); ?>" alt="<?php the_title();?>" /> 
             </a> 
            <?php } else { ?> 
             <a href="<?php the_permalink();?>"> 
              <img src="<?php echo get_template_directory_uri() . '/img/missing_56.png';?>" alt="<?php the_title();?>" /> 
             </a> 
            <?php } ?> 
           </figure> 
           <p> 
            <a href="<?php the_permalink();?>"><?php the_title();?></a> <br /> 
            <span> <?php _e('Le ', 'Voxis'); the_time("F d, Y");?>, <?php comments_popup_link(esc_html__('0 commentaires','Voxis'), esc_html__('1 commentaire','Voxis'), '% '.esc_html__('commentaires','Voxis')); ?> </span> 
           </p> 
          </a> 
         </li> 
        <?php endwhile; wp_reset_postdata(); ?> 
       </ul> 

____________________________________________________________________________ 
for last 7 days post use this code: 

<?php 
function filter_where($where = '') { 
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
    return $where; 
} 
add_filter('posts_where', 'filter_where'); 
query_posts('post_type=post&posts_per_page='.$number_posts.'&orderby=comment_count&order=DESC'); 
?> 

<?php while (have_posts()) : the_post(); ?> 

        <li> 
         <a title="<?php the_title();?>" href="<?php the_permalink();?>"> 
          <figure> 
           <?php if(has_post_thumbnail()) { ?> 
            <a href="<?php the_permalink();?>"> 
             <img src="<?php echo aq_resize(wp_get_attachment_url(get_post_thumbnail_id($post->ID)), 42, 42, true); ?>" alt="<?php the_title();?>" /> 
            </a> 
           <?php } else { ?> 
            <a href="<?php the_permalink();?>"> 
             <img src="<?php echo get_template_directory_uri() . '/img/missing_56.png';?>" alt="<?php the_title();?>" /> 
            </a> 
           <?php } ?> 
          </figure> 
          <p> 
           <a href="<?php the_permalink();?>"><?php the_title();?></a> <br /> 
           <span> <?php _e('Le ', 'Voxis'); the_time("F d, Y");?>, <?php comments_popup_link(esc_html__('0 commentaires','Voxis'), esc_html__('1 commentaire','Voxis'), '% '.esc_html__('commentaires','Voxis')); ?> </span> 
          </p> 
         </a> 
        </li> 


<?php endwhile; 
     wp_reset_query(); 
?> 
+0

你好謝謝Answear它的工作很好真的謝謝soo糊。 最後一個問題,如果我想按周來做,我應該改變什麼? – Dolomats

+0

嗨,我添加了最後7天后使用代碼添加在波紋月查詢。 –