【问题标题】:Weekly Post in wordpresswordpress 中的每周帖子
【发布时间】:2014-07-28 11:31:52
【问题描述】:

我有一个 WordPress 网站并且有一些帖子。我在“祝福”类别下有一些特别的帖子。我想通过锚标签显示本周发布的祝福类别中的所有帖子。我想在主页上放一张照片当用户点击它时,它只会显示本周祝福类别的帖子。年份可以是任何...意味着不需要年份,我只想要本周的帖子.. 请帮忙.. 编码会很有帮助...我是 WordPress 新手... 请帮忙。

我输入了一些代码...请帮忙..它显示了一周的帖子..但都不是我的类别

<?php
$week = date('W');
$args = array(
'w' => $week,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 'blessing',
'date_query' => array('week' => $current_week),
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;

wp_reset_query();  // Restore global post data stomped by the_post().
?>

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    你需要使用 date() 函数 (PHP DATE);

    $current week = date("W");
    

    //$current_week 将输出当前周数

    然后你做post query,从wordpress 3.7你可以使用“date_query”(WP_QUERY#Date_parameters);

    query_posts(array(
    'cat' => 'Blessing', // chosing category
    'date_query' => array('week' => $current_week), // choosing date range
    'posts_per_page' => '-1'// outputs all posts
    ));
    while (have_posts()) : the_post();
    //here goes your loop
    endwhile;
    

    在您的模板文件中使用它。 并且不要忘记输入 wp_reset_query();在你的查询完成之后,如果你有循环之后。

    如果你在 3.7 下使用 wordpress,你可以使用这个答案:Wordpress - get posts by date range,但在这里你需要获取当前周的第一个和最后一个日期(你可以使用这个Finding First day of week via php [duplicate])。 祝你有美好的一天。

    【讨论】:

    • 感谢代码和回复。我在上面的问题中输入了一些代码。它按类别显示每周帖子不起作用...请帮助...我正在使用 wordpress 3.8.3
    猜你喜欢
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多