WordPress uses different header files for different pages

Source: Internet
Author: User
Tags php file

How to define different header files for different categories in WordPress

You don't often encounter this kind of situation, do you want to apply different head styles to different classifications? I'm sure you have this kind of demand. Here are some simple and effective solutions that allow you to define different head styles for carbonized themes. If you need more details, you can check out the WordPress development documentation.

First you have to create different header files for different categories, and you need to have a header file that is the default. These files will be included in the main header.php file. The following code is the code required by the WordPress theme template.

<?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 ');?>
}
}
?>

The following are examples of names and aliases used in categories

<?php
Alternative for using Category ID, can be 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 ');?>
}
}
?>

You may also want to use different header files for different pages, so I've also written the following code examples.

<?php
//
Example of using header file based on pages
Note This 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 ');?>
}
?>

Hint: You need to use the above code to write your header.php file to load the special head element according to your actual situation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.