wordpress tips tricks

Don’t you come across situations like you need to implement multiple header types for different categories? I’m sure you do. Here is a simple yet effective solution to work out the issue to implement multiple header types for different categories. For more conditional tags you can look up to WordPress codex.

First of all create the header files for each categories. Also have one header file as the default one. These files will be included from the main header.php file. Consider the code below for the your WordPress theme header.php file.



/**** Conditional Header for Per Category Example WordPress ****/

// make sure we are in category listing
if (is_category()){

if (is_category('1')){

}
elseif (is_category('2')){

}
else {
// this is the deafult header

}
}
?>

Example of using Category Names and Slugs

// Alternative for using Category ID, You can use Category Name or Slug
if (is_category()){

// example of using a category name
if (is_category('Category 1')){

}
// example of using category slug
elseif (is_category('category-1')){

}
else {
// this is the deafult header

}
}
?>

Alternatively you might want different headers for different pages, so I’ve included that example too.


//
// example of using header file based on pages
// note that you can either use page ID, Page Name or Page Slug
//

// this one uses page title
if (is_page('About')){

}
// this one uses page slug
elseif (is_page('subscribe')){

}
// this one uses page ID
elseif (is_page('6')){

}
else {

}
?>

Reminder: You need to use these codes inside your themes header.php file to load specific header elements based on condition.