0
votes

To set a different header per page in WordPress, I usually edit php get_header(); line in theme files such as index.php, page.php...

I wonder if it's possible to change the header file per page with 'get_header' action without editing theme files such as page.php.

I tried the following code, but it didn't work.

function themeslug_header_hook( $name ) {
    if(is_front_page() || is_home()) {
$name = 'home';
}
$return $name;
}
add_action( 'get_header', 'themeslug_header_hook' );

Is there any way to set a different header per page within the functions.php file?

Thanks.

1

1 Answers

1
votes

I assume you could always do something like this in your functions file.

function get_my_header() {

   if( !is_home() ) {

      global $post;
      // get category by ID
      $category = get_the_category($post->ID);
      // first category slug
      $catslug = $category[0]->slug; 

      // is there is a category slug call the header-$catslug.php
      if (isset($catslug)) {
             get_header($catslug);
      } else {
         // else call normal header.php
             get_header();
       }// ends !is_home()

   // else call normal header
   } else {
      get_header();
   }// ends isset()

} // ends get_myheader function