Wordpress:本周或月份的大多数人选帖子

时间:2013-07-03 16:03:26

标签: wordpress post

我正在使用一个主题,显示下面代码的热门帖子,我需要 有人帮我编辑这段代码,让它在上周显示热门帖子或 一个月。

<?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>

2 个答案:

答案 0 :(得分:1)

我尚未测试下面的代码,但会确保这对您有用。

<?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
?>

答案 1 :(得分: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();
?>