我是第一次在Wordpress上发布版面,所以如果问题太简单、太明显,我很抱歉:
我有一个即将到来的主修课的时间表在页面上。我需要按日期的升序显示文章(日期在文章标题中显示,或者,如果更方便,我可以创建一个带有日期的自定义日期字段)。日期格式为DD.MM.YY
此外,也有必要不显示带有过去日期的帖子。
请告诉我如何实现这一点?
下面是我当前的帖子代码:
<?php
$args = array(
'numberposts' => 0,
'category_name' => schedule,
'orderby' => 'title',
'order' => 'ASC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'suppress_filters' => true,
);
$posts = get_posts( $args );
foreach($posts as $post){ setup_postdata($post);
?>
<div class="col-md-4 col-sm-6">
<div class="content-wrapper bg-gray schedule-card">
<div class="day"><span><?php the_field('week_day') ?></span><span><?php the_title() ?></span></div>
<a class="event" href="<?php the_field('mc_descr_link_1') ?>"><span class="event-time"><?php the_field('time-mc-1') ?></span><span class="event-name"><?php the_field('mc-1') ?></span></a>
<a class="event" href="<?php the_field('mc_descr_link_2') ?>"><span class="event-time"><?php the_field('time-mc-2') ?></span><span class="event-name"><?php the_field('mc-2') ?></span></a>
<a class="event" href="<?php the_field('mc_descr_link_3') ?>"><span class="event-time"><?php the_field('time-mc-3') ?></span><span class="event-name"><?php the_field('mc-3') ?></span></a>
</div>
</div>
<?php
}
wp_reset_postdata();
?>发布于 2018-06-14 07:36:14
最好为这些日期字段创建自定义字段。或者您可以直接将该日期字段与本机“日期”字段集成。但是,如果希望将它们与post_date字段分开,则需要实现以下内容:
注意,我还从上面的数组中删除了不必要的参数。
发布于 2018-06-14 09:44:37
如果您使用自定义字段进行日期:
$today = date('Y-m-d');//date format need to change according to your records
$args = array(
'numberposts' => 0,
'category_name' => schedule,
'meta_key' => 'CUSTOM_DATE_FIELD_NAME',
'meta_type'=>'DATE',
'meta_query' => array(
array(
'key' => 'CUSTOM_DATE_FIELD_NAME',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value_date',
'order' => 'ASC'
'exclude' => array(),
'post_type' => 'post',
'suppress_filters' => true,);
发布于 2018-06-14 12:06:11
将CUSTOM_FIELD_DATE格式更改为Y-m-d Ex:2017-02-27
$today = date('Y-m-d');//Format is 2018-01-28
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'meta_key'=>'date_field',
'meta_type'=>'DATE',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_field',
'value' => $today,
'compare' => '>='
)
),
);
$posts= new WP_Query( $args );
foreach($posts->posts as $post){ setup_postdata($post);
?>
<div class="col-md-4 col-sm-6">
<div class="content-wrapper bg-gray schedule-card">
<div class="day"><span></span><span><?=$post->post_title;?></span></div>
</div>
</div>
<?php}
wp\_reset\_postdata();https://stackoverflow.com/questions/50851361
复制相似问题