我正在尝试在小部件部分显示上周的3条帖子
我使用了此代码
function wpb_last_week_posts() {
$thisweek = date('W');
if ($thisweek != 1) :
$lastweek = $thisweek - 1;
else :
$lastweek = 52;
endif;
$year = date('Y');
if ($lastweek != 52) :
$year = date('Y');
else:
$year = date('Y') -1;
endif;
$the_query = new WP_Query('year=' . $year . '&w=' . $lastweek );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<br>
<style>
.weeklyimg {
float:left;height:auto;width:100px;margin-right: 10px;
}</style>
<br>
<a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_post_thumbnail('thumbnail', array('class' => 'weeklyimg attachment-post-thumbnail size-post-thumbnail wp-post-image no-display appear')); ?></a>
<h6><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h6>
<span class="entry-date" style="
float: right;
width: 52px;
font-weight: 600;
font-size: 11px;
padding: 5px;
background: #f5f5f5;
color: #757575;
"><?php echo get_the_date(); ?></span>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
}
add_shortcode('lastweek', 'wpb_last_week_posts');
它奏效了,但我不知道如何设置多个帖子。
我尝试在 <?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
}
add_shortcode('lastweek', 'wpb_last_week_posts');
上添加posts_per_page=3
,但是它只能部分起作用,因为我没有收到上周的帖子,而是过去24小时的帖子:/ ..
也许有人可以帮我这个忙。
谢谢。