简体   繁体   English

如何使Wordpress帖子输出按帖子标题中指定的日期排序? 以及如何显示标题为> =今天的日期的帖子?

[英]How to make Wordpress posts output sorted by the date specified in the post title? And how to display posts with a date in the title > = today's date?

I'm the first time I'm putting out the layout on Wordpress, so I apologize if the question is too simple and obvious: 我是第一次在Wordpress上发布版式,因此如果问题过于简单和显而易见,我深表歉意:

I have a schedule for the upcoming master classes on the page. 我在页面上为即将举行的大师班制定了时间表。 And I need the posts to be displayed in ascending order of the date (the date is indicated in the post's title, or, if it is more convinient, I can create a custom date field with the date). 而且我需要帖子以日期的升序显示(日期显示在帖子标题中,或者,如果更方便,我可以使用日期创建自定义日期字段)。 Date format is DD.MM.YY 日期格式为DD.MM.YY

And also, it is necessary that posts with past dates will be not displayed. 而且,有必要不显示具有过去日期的帖子。

Please, tell me how to implement this? 请告诉我如何实施?

Here is my current code for posts: 这是我当前的帖子代码:

<?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();
  ?>

It would be better if you create custom field for those date fields. 如果为这些日期字段创建自定义字段,那就更好了。 Or you can directly integrate that date field with native "date" field. 或者,您可以直接将该日期字段与本机“日期”字段集成。 But if you want to keep them separate from post_date field, then here is what you need to implement: 但是,如果要将它们与post_date字段分开,则需要执行以下操作:

  1. Create some custom field which contains those date values you are telling about. 创建一些自定义字段,其中包含您要讲述的那些日期值。 (CUSTOM_FIELD_WHICH_CONTAINS_DATE_VALUE) (CUSTOM_FIELD_WHICH_CONTAINS_DATE_VALUE)

  2. Change $args array to this: 将$ args数组更改为此:

     $args = array ( 'category_name' => 'schedule', 'orderby' => 'meta_value', 'meta_key'=>'CUSTOM_FIELD_WHICH_CONTAINS_DATE_VALUE', 'order'=>'ASC' 'post_type' => 'post'); 

Note that i have also removed unnecessary arguments from the array above. 请注意,我还从上面的数组中删除了不必要的参数。

If you are using custom field for date: 如果您使用自定义字段作为日期:

$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,

); );

Change your CUSTOM_FIELD_DATE format to Ymd Ex:2017-02-27 将您的CUSTOM_FIELD_DATE格式更改为Ymd 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(); wp_reset_postdata();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM