experience

经验

zw-1988.com

home > 经验 > wordpress 对日期进行分类链接

2018.1.18 wordpress 对日期进行分类链接

获日志的添加时间和链接

$year = get_the_time( 'Y' );      //获取日志的年
$month = get_the_time( 'm' );       //获取日志的月
$day = get_the_time( 'd' );    //获取日志的日
<?php echo get_day_link( $year, $month, $day ) ; ?> //输出时间
<a href="' . get_day_link( $year, $month, $day ) . '" title="查看所有' . esc_attr( get_the_time( 'Y年m月d日' ) ) . '文章">' . $day . '日</a>

WP_Query 属性中的date_query

<?php
          $date_query=array(
                        array(
                          'column' => 'post_date',
                        'before' => date("Y-m",strtotime("last day of +3 month", time())),
                      'after' =>date('Y-m',strtotime("last day of -1 month", time()))
                                  )
         );
       $student_items_args = array(
                    'post_type' => 'student',
                    'posts_per_page' => -1,
                    'orderby'=>'date',
                    'order'=>'ASC',
                    'date_query' => $date_query,
                    'tax_query' => array(
		              array(
                        'taxonomy' => 'taxonomy-student',
                        'field' => 'id',
                        'terms' => 14,
		              ),
                    ),
       );
$student_items_query = new  WP_Query($student_items_args);
if ($student_items_query->have_posts()) {
    while ($student_items_query->have_posts()) {
        $student_items_query->the_post();
    $student_link = get_post_meta($post->ID, '_student_link', true);
        ?>
                    <li>
                        <a href="<?php echo $student_link;?>">
                            <?php echo get_the_title();?>
                        </a>
                    </li>

                    <?php
    }
    wp_reset_postdata();
}
?>


这个例子讲的是要检索未来3个月内容,本来是直接用3*30*24*60*60 来算的 但是后来考虑到不是每个月都30天,所以具体想了下用了3 month ,但是单独用这个代码出现的是更新日期开始算的3个月,所以最后加个last day 是表示当月最后一天开始算起。

7天的倒是可以直接用固定时间算:7天之内的表示添加‘newtag’

$post_date = strtotime(get_the_date("Y-m-d")) ;
$cur_date = strtotime(date("Y-m-d ")) - 7*24*60*60;

<?php if( $post_date <= $cur_date ){ echo 'newtag';};?>

 

7天内显示new

<?php $post_date = date("d ")-get_the_date("d") ;
if( $post_date<=7 ){ echo '<span>new</span>';}?>
«
相关文章
wordpress (25)java script (9)tool (8)css (2)攻略 (1)第一次人体 (1)插件 (1)ECharts (1)food (1)雪糕 (1)动画 (1)book (1)标签云 (1)生活 (1)一言难尽的画稿 (1)
辽ICP备16002031号

Copyright © 2016 - 2020 1988-zw.com