0

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?

2 Answers 2

1

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.

1
  • You're welcome. If you could, please click on the check to mark it as the answer.
    – Cedon
    Commented Jan 17, 2018 at 0:36
-1

You could create another header called header-blog.php

Then on your blogs template replace the

 <?php
    get_header();
?>

with

  <?php
if ( is_home() ) :
    get_header( 'blog' );
else
    get_header();
endif;
?>

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

1
  • 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
    Commented Jan 15, 2018 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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