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

7 thoughts on “How To Display A Message On Every Post In WordPress Astra”

  1. That's very useful thank you. Could this be used to add something like this -
    Updated [post_modified_date] : First Published [post_date] [post_edit]

    I tried it but couldn't get it to work

    Reply
  2. I was using this code successfully for a few weeks, but it doesn't seem to be working anymore. It shows up on every page, not just the single posts.

    Reply
    • Make sure it's wrapped in if( is_single() ) {}

      I just tested it with the most recent version (3.7.10) and it's working fine. if( is_single() ) {} is a WordPress check, not specific to Astra.

      If that doesn't work, feel free to paste your code for this function here and we'll see where it is going wrong.

      Reply
      • I figured it out!

        Ideally I would like to display my message under (or in) the post meta but before the featured image. (my post format is title/meta, featured image, content.)

        But when I use the "astra_entry_content_before" hook, it shows up under the featured image.

        Is there a way to add a custom hook location, or do this another way?

        Reply

Leave a Comment