最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to apply CSS based on publishing date

programmeradmin1浏览0评论

Is there an idea in WordPress that can apply CSS file only to posts published after a certain date?

Renewal a site created with WordPress. It is to keep the previous record and apply the design differently after the renewal.

Is there an idea in WordPress that can apply CSS file only to posts published after a certain date?

Renewal a site created with WordPress. It is to keep the previous record and apply the design differently after the renewal.

Share Improve this question edited Dec 31, 2020 at 1:03 Dev lucasdesign asked Dec 30, 2020 at 3:19 Dev lucasdesignDev lucasdesign 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can compare the post date to the date of your choice and enqueue different styles depending on the result:

<?php

function my_enqueue_styles_depending_on_date() {

    global $post;

    $style_switch_date = '20201230'; // the trigger date
    
    if ( is_single( $post->ID ) ) {

        $post_date = get_the_date( 'Ymd', $post->ID );

        if ( $post_date > $style_switch_date ) {
            wp_enqueue_style( 'css_for_newer_posts' );
        } else {
            wp_enqueue_style( 'css_for_older_posts' );
        }
    }
}

add_action('wp_enqueue_scripts', 'my_enqueue_styles_depending_on_date');

Make sure to wp_register_script() before you enqueue the style by it's name (handle) or use the full wp_enqueue_style() syntax.

发布评论

评论列表(0)

  1. 暂无评论