【问题标题】:Wordpress group posts by weekWordPress 群组帖子(按周)
【发布时间】:2014-08-16 12:03:38
【问题描述】:

我正在寻找一种按周显示和浏览 wordpress 帖子的方式。

我将每周发布 4-6 个帖子,我希望主页显示该周的帖子。之前的每一页都应该显示一组按周排序的帖子。

每周的帖子数量不是固定的,并且会有所不同,所以我不知道该怎么做。

提前致谢!

【问题讨论】:

  • 太好了。祝你好运。

标签: php wordpress loops posts


【解决方案1】:

这样的东西是你的追求

<?php 
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) : 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

【讨论】:

  • 非常感谢,这似乎工作得很好——唯一的问题是分页。我该怎么办?
【解决方案2】:

不确定这是否可行,但未经过测试

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'posts_per_page' => 50, 'paged' => $paged, 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) : 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

【讨论】:

  • 我刚试过,但现在由于某种原因,所有帖子都加载到主页上?
  • 你把posts_per_page改成你想要的数字了吗?
  • 我现在这样做了,但主要问题是我不知道每周会发布多少帖子,它总是会有所不同(这就是为什么我无法使用“正常”wordpress 循环)
  • 如果您将其设置为每页 5 个,那么无论您有多少帖子,它只会为每 5 个帖子创建一个分页
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2019-02-15
相关资源
最近更新 更多