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 badges1 Answer
Reset to default 2You 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.