Need to display a limited-time offer, announcement, or custom message across your blog posts – without editing every post or touching your theme files?
With just a few lines of code, you can dynamically inject HTML before or after post content, conditionally based on post type, category, date, or anything else you need.
Table of contents
Use case
Imagine you’re running a seasonal promotion – say, a summer sale that gives customers 25% off services for one week.
You want to advertise this offer across all blog posts in your “Deals” and “Tips” categories. But you don’t want to manually edit each post, and you definitely don’t want the message to stick around after the promotion ends.
This is a perfect situation where a simple filter using the_content
can inject that promotional message automatically, only while it’s relevant.
Snippet
This snippet adds a custom promotional banner to WordPress posts under specific conditions. It checks if the post is a standard blog post, ensures it’s the main query, verifies that the current date is before a set promotion end date, and limits display to posts within selected categories. When all conditions are met, a styled HTML banner is injected above the post content.
Not sure how to use a snippet? Find out how to add one.
function yourprefix_promo_banner( $content ) {
if ( is_singular( 'post' ) && is_main_query() ) {
// Set your condition - example: a promo that's active until July 31, 2025
$promo_end_date = strtotime( '2025-07-31' );
$current_time = current_time( 'timestamp' );
// Check if promo is active
if ( $current_time <= $promo_end_date ) {
// Check if post is in specific categories
if ( has_category( array( 'deals', 'tips' ) ) ) {
// HTML output
$banner = '<div class="promo-banner" style="background:#ffe59e;padding:15px;border-radius:8px;margin-bottom:20px;text-align:center;">
<strong>🔥 Limited-Time Offer:</strong> Get <span style="color:#d9534f;">25% off</span> all services this week only! <a href="#">Learn more</a>.
</div>';
// Return the HTML and the post content
return $banner . $content; // Switch this around to have the content below, or include $banner twice for before and after
}
}
}
return $content;
}
add_filter( 'the_content', 'yourprefix_promo_banner' );
Preview

As shown above, the banner appears exactly as intended – dynamically positioned based on your chosen conditions and post types.
Customisation
- Adjust the display conditions – for example, show the banner only on Mondays instead of up to a specific date.
- Target specific post types using
is_singular('custom_post_type')
. - To display the banner after the post content, modify the return statement to:
return $content . $banner;
- To display the banner before and after the post content, use:
return $banner . $content . $banner;
With these adjustments, you can fully control where, when, and how your banner appears – making it more relevant and effective for your audience.