Is there a way to put some code into the header but have it load only on blog posts?

Maybe a plugin or a piece of code that could help me accomplish this?


Yes. In your functions.php file, add something like the following:

function my_post_header_function() {
   if( is_single() ) {
      // Your Code Goes Here
add_action( 'wp_head', 'my_post_header_function' );

What this will do is execute this when wp_head() is fired. It will see if you're on a single post (NOTE: This will not work on pages or attachments) and if you are, it will execute the code you want to put.

| improve this answer | |
  • You're welcome. If you could, please click on the check to mark it as the answer. – Cedon Jan 17 '18 at 0:36

You could create another header called header-blog.php

Then on your blogs template replace the



if ( is_home() ) :
    get_header( 'blog' );

I know is_home is slightly confusing but this is what wordpress looks for as a blog page

also see https://codex.wordpress.org/Function_Reference/get_header

| improve this answer | |
  • It's a little overkill to create a completely different header.php file for only inserting a few lines of extra code. Plus it adds some maintainability problems down the road. – Cedon Jan 15 '18 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.