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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /**** Conditional Header for Per Category Example Wordpress ****/ // make sure we are in category listing if (is_category()){ if (is_category('1')){ <?php include(TEMPLATEPATH.'/header-cat1.php'); ?> } elseif (is_category('2')){ <?php include(TEMPLATEPATH.'/header-cat2.php'); ?> } else { // this is the deafult header <?php include(TEMPLATEPATH.'/headerdefault.php'); ?> } } ?> |
Example of using Category Names and Slugs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php // 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')){ <?php include(TEMPLATEPATH.'/header-cat1.php'); ?> } // example of using category slug elseif (is_category('category-1')){ <?php include(TEMPLATEPATH.'/header-cat2.php'); ?> } else { // this is the deafult header <?php include(TEMPLATEPATH.'/headerdefault.php'); ?> } } ?> |
Alternatively you might want different headers for different pages, so I’ve included that example too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php // // 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')){ <?php include(TEMPLATEPATH.'/header-contact.php'); ?> } // this one uses page slug elseif (is_page('subscribe')){ <?php include(TEMPLATEPATH.'/header-subscribe.php'); ?> } // this one uses page ID elseif (is_page('6')){ <?php include(TEMPLATEPATH.'/header-contact.php'); ?> } else { <?php include(TEMPLATEPATH.'/headerdefault.php'); ?> } ?> |
Reminder: You need to use these codes inside your themes header.php file to load specific header elements based on condition.



Pingback: 在wordpress中如何为不同的分类定义不同的头部文件 - 收集分享互联网资源!