如何在WordPress发布文章两周后用PHP隐藏div标签(无需JS)
Hey there, let's solve this problem properly. You want to hide a specific div on WordPress posts two weeks after they're published, using only PHP (no JavaScript)—totally doable. Here are two approaches depending on where your div is located:
1. For Static Divs in Your Theme Template
If the div is hardcoded into your theme files (like single.php, page.php, or a custom template), wrap it in a PHP conditional check that compares the post's publish time to the current time.
Step-by-Step Code Implementation:
<?php // Get the post's publish timestamp (U returns Unix timestamp) $post_publish_time = get_post_time('U', true, get_the_ID()); // Calculate two weeks in seconds (14 days × 24h × 60m × 60s) $two_weeks = 14 * 24 * 60 * 60; // Get current WordPress timestamp (respects your site's timezone settings) $current_time = current_time('timestamp'); // Only show the div if less than two weeks have passed if ($current_time - $post_publish_time < $two_weeks) { ?> <!-- Your target div goes here --> <div class="your-target-div"> This content will only be visible for the first two weeks after publishing! </div> <?php } ?>
Key Notes:
- Edit the correct template file: For single posts, use
single.php; for custom post types, usesingle-{post-type}.php(replace{post-type}with your CPT slug). - Timezones are handled automatically: WordPress uses the timezone you set in Settings > General, so no extra conversion is needed.
- Always use a child theme to modify templates—this prevents your changes from being overwritten when the parent theme updates.
2. For Divs Inside Post Content
If the div is part of the post content (added via the block editor or classic editor), use a the_content filter to strip it out once the two-week mark passes.
Step-by-Step Code Implementation:
Add this code to your theme's functions.php file (or a custom plugin if you prefer):
add_filter('the_content', 'hide_div_after_two_weeks_in_content'); function hide_div_after_two_weeks_in_content($content) { // Only run this on single post pages if (!is_single()) { return $content; } // Get post publish time and current time $post_publish_time = get_post_time('U', true); $two_weeks = 14 * 24 * 60 * 60; $current_time = current_time('timestamp'); // If two weeks have passed, remove the target div if ($current_time - $post_publish_time >= $two_weeks) { // Replace this selector with your div's identifier (class or ID) // Example: Removes divs with class "expiring-content" $content = preg_replace('/<div class="expiring-content">(.*?)<\/div>/s', '', $content); } return $content; }
How to Use This:
When adding the div to your post content, give it the class you specified in the filter (e.g., expiring-content):
<div class="expiring-content"> This content will disappear two weeks after the post is published. </div>
Key Notes:
- The regex pattern uses the
smodifier to match content across multiple lines. - If you need to target an ID instead of a class, update the regex to:
/<div id="expiring-content">(.*?)<\/div>/s - Test the regex thoroughly to make sure it only removes the intended div and no other content.
内容的提问来源于stack exchange,提问作者Jack

