How To Display A Message On Every Post In WordPress Astra

I had a request from a reader for a tutorial on how to add a message to every post, but not to archives, categories or the blogroll.

Specifically, they wanted an affiliate disclaimer to appear on every post, below the post title. They were using the Astra theme.

Example of a post showing disclaimer below the title

But they didn't want the affiliate disclaimer to appear on the post preview on category etc pages.

Example of a category page with the lack of the message under the title highlighted

How to add text to every post on WordPress Astra

Create a function that hooks into the place in the post you want the text to appear. Use if( is_single() ) {} to only display the text on single posts.

Because I wanted the text to display before the content, but under the post title, the code I used is:

add_action( 'astra_entry_content_before', 'display_disclaimer' );

function display_disclaimer() {
  if( is_single() ) {
    echo "<p><em>This is a disclaimer</em></p>";
  }
}

Add this code to the functions.php file of your child theme.

But what if you wanted to do the opposite?

How to add text on the category page for every post using WordPress Astra

Create a function that hooks into the place in the post you want the text to appear. Use if( is_archive() ) {} to only display the text on the category pages. Or use if( !is_single() ) {} to display the text everywhere that isn't a single post.

The code is almost identical to above:

add_action( 'astra_entry_content_before', 'display_disclaimer' );

function display_disclaimer() {
  if( is_archive() ) {
    echo "<p><em>This is a disclaimer</em></p>";
  }
}

That will add the text to every post, but only on the category pages.

Example of a message displayed below the title on category pages

Mike Haydon

Thanks for checking out my WordPress and coding tutorials. If you've found these tutorials useful, why not consider supporting my work?

Buy me a coffee

Leave a Comment