【问题标题】:Wordpress: List Posts by Day, with that day being the titleWordpress:按天列出帖子,以当天为标题
【发布时间】:2013-10-01 07:01:53
【问题描述】:

我之前尝试过这样做但失败了,也许我只是不明白它是如何工作的,也许以前有人这样做过并且可以帮助我。

用 wordpress 为一所学校建立一个网站,他们有一个科目的课程,他们按一周中的几天列出它们,如下所示:

星期一

  • 数学
  • 英语
  • 科学

星期二

  • 科学

星期三

  • 数学
  • 历史

当我构建这个时,我使用了一个名为“classes”的自定义帖子类型,然后我使用自定义帖子字段来输入所有数据,包括时间()格式(使用日期选择器)

这就是我回显日期/时间的方式: <?php echo(types_render_field("date-and-time", array("format"=>"l","arg2"=>"val2"))); ?>

现在,这是我的循环:

GIST of Loop

但它根本不起作用,它列出了日子,但它会多次列出日子,所以如果星期三有 4 个帖子,它会列出星期三、星期三、星期三、星期三。

这是有道理的,因为它只是循环浏览帖子并回显它的日期。

基本上,我正在尝试写:

- loop days of the week 7 days ahead 
    - loop posts under the date heading based on custom field date

我原本打算为此使用预定发布,但在 wordpress 中安排发布对于不懂计算机的人来说并不是最简单的,使用自定义字段,我可以设置样式并使用日期选择器TYPES 提供。

如果有人可以帮助我,那就太好了!

【问题讨论】:

  • 尝试将the_title 放在循环之外。
  • 你能解释一下那会做什么吗?
  • the_title() 打印您当前帖子/页面的标题
  • 我知道的很多,但是将其保留在循环之外根本不会将其附加到循环中,这意味着没有“类”帖子类型查询的约束,它只是显示来自标准博客的最后一个“帖子”标题。

标签: wordpress nested-loops


【解决方案1】:

我的朋友设法为我的问题创建了一个绝妙的解决方案,我将在此处发布代码并希望它对将来的某人有所帮助。

<?php 
 $begin = strtotime('Monday last week'); // Year . Week
 $end = $begin + (21 * 24 * 60 * 60);
 $week = -1;
 $day = -1;
 $table = FALSE;
 $this_week = date('W');

query_posts( 
array( 
    'post_type' => 'classes',               // Custom Post Type
    'order' => 'asc',                       // Order with earliest first
    'orderby' => 'wpcf-date-and-time',      // Order by the custom field
    'posts_per_page' => -1,                 // Show all
    'meta_key' => 'wpcf-date-and-time',     // Key is the custom field
    'meta_value' => array($begin, $end),    // Array of the current month and next
    'meta_compare' => 'BETWEEN',            // Between Being and End
    'author' => $author_id                  // Show only the author
    ) 
);                                          // Get posts between now and then

if (have_posts()) : while (have_posts()) : the_post(); 
    $do_not_duplicate = $post->ID;
    $time = types_render_field("date-and-time", array("arg1"=>"val1","raw"=>"true")); // Your custom field
    $current_week = date('W', $time);
    $current_day = date('l', $time);
    if ($current_week > $week) {
        if ($table) {
            echo "</table>";
            $table = FALSE;
        }
        $week = $current_week;
        if ($week > $this_week)          { $week_str = 'Next week';}
        else if ($week < $this_week) { $week_str = 'Last week';}
        else                                                 { $week_str = 'This week';}
        echo '<h2 class="">' . $week_str . '</h2>';
    }
    if ($current_day != $day) {
        $day = $current_day;
        if ($table) {
            echo "</table>";
            $table = FALSE;
        }
        $table = TRUE;
        echo '<h3 class="">' . $day . '</h3>';
        echo '<table>
                <thead>
                    <tr>
                        <td>Location</td>
                        <td>Venue</td>
                        <td>Time</td>
                        <td>age</td>
                    </tr>
                </thead>';
    }
    ?>

<tr>
    <td><?php the_title(); ?></td>
    <td><?php echo(types_render_field("location", array("arg1"=>"val1","arg2"=>"val2"))); ?></td>
    <td><?php echo(types_render_field("venue", array("arg1"=>"val1","arg2"=>"val2"))); ?></td>
    <td><?php echo get_the_term_list( $post->ID, 'age-group', '', ', ', '' ) ?></td>
</tr>   

<?php endwhile; else: ?>
<p>We don't currently have any classes available on this day,
but we can <strong>book your child in for a class</strong>, please get in touch to organise this</p>
 <?php endif; wp_reset_query() ?>                                               

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多