1

我正在使用一个主题,该主题使用以下代码显示热门帖子,我需要有人帮助我编辑此代码以使其显示上周或上个月的热门帖子。

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

2 回答 2

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
?>
于 2013-07-03T16:53:41.120 回答
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();
?>
于 2013-07-04T09:49:27.370 回答