Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

The method of calling different templates in different categories of website CMS

What this article is going to introduce is that the two functions are different classification templates and different articles calling different templates. The editor of this article has organized two articles, I hope it will be helpful to you.

Different categories of wordpress call different template files

When using wordpress as a website, especially an enterprise website, the content of multiple categories needs to be displayed differently. For example, some categories display product thumbnails, and some display company news announcements. At this time, different categories are required. Templates, although wordpress has only one general category template by default, but different categories can be called through function code to call different templates.

How to implement different categories of WordPress to call different templates:

In the category.php file of the current WordPress theme (create a new one if you don't have one), add the following judgment code:

PHPCodeCopy content to clipboard
  1. $ post = $ wp_query->post;
  2.     if(in_category('1')) {
  3.     include(TEMPLATEPATH.'/category-1.php');
  4.     }
  5.     else if (in_category('2')) {
  6.     include(TEMPLATEPATH./'category-2.php');
  7.     }
  8.     else {
  9.     include(TEMPLATEPATH.'/category-3.php');
  10.     }
  11. ?>

The judgment function means: judge whether the category ID is '1', if so, call the category-1.php template, if the category ID is 2, call the category-2.php template, if the above two are not, call the category- 2.php template

Reminder: Similar methods can also be used to implement different articles calling different pages, different archives calling different pages, etc.

When making wordpress multi-column templates, I encountered different categories calling different templates. If my blog has category PHP technology, ASP technology, and each category needs to call different category templates, I found a simple calling method, the following is the Let Summer Blog teach you how to invoke the different categories.

XNUMX. Different categories call different templates

PHPCodeCopy content to clipboard
  1. <?php
  2. $ post = $ wp_query->post;
  3. if ( in_category('7') )
  4. {
  5. include(TEMPLATEPATH . '/archive-view.php'); }
  6. else if ( in_category('12') )
  7. {
  8. include(TEMPLATEPATH . '/single12.php');
  9. }
  10. else if ( in_category('42') )
  11. {
  12. include(TEMPLATEPATH . '/single42.php');
  13. }
  14. else {   include(TEMPLATEPATH . '/archive-other.php');
  15. }
  16. ?>

The above code is taken from a piece of code on the Internet. Just put the code into the category file archive.php of the theme. The number code in the middle of in_category('7') is the ID of the category, which is in the background category directory. As you can see, archive-view.php is a template file, you need to create a new single-view.php file in the template before you can call it.

XNUMX. Different articles call different templates according to their categories

Different templates are called in different categories, and the articles in the following categories also need to call the templates of the current category. See the implementation method below.

PHPCodeCopy content to clipboard
  1. <?php
  2. $ post = $ wp_query->post;
  3. if ( in_category('7') ) {
  4. include(TEMPLATEPATH . '/single-view.php');
  5. }
  6. else if (in_category('3'))
  7. {
  8. include(TEMPLATEPATH . '/single-CASE​.php');
  9. }
  10. else if ( in_category('42') )
  11. {
  12. include(TEMPLATEPATH . '/single42.php');
  13. }
  14. else {   include(TEMPLATEPATH . '/archive-other.php');
  15. }
  16. ?>

It is basically the same as the files that call different templates in different categories of wordpress, that is, the template file single-view.php to be called is different. This also needs to be newly created so that it can be called.Of course, this code should also be placed in the article template page single.php.

XNUMX. The home page calls different templates.

If you need to call a separate template for the home page, you can add the following code to the archive.php file to achieve it.

PHPCodeCopy content to clipboard
  1. <?php
  2. if (is_home() || is_front_page()) { ?>
  3. include(TEMPLATEPATH . '/home.php');

If you need different single pages or website tags to call a separate template, the above parsing code can be used on the template of page and tag, you can try to modify it yourself~~

Different articles in wordpress call different template files

Find the following code in the article page single.php:

PHPCodeCopy content to clipboard
  1. <?php
  2. get_header ();
  3. ?>

Modify it to the following code:

PHPCodeCopy content to clipboard
  1. <?php
  2. $header = get_post_meta($ post->ID, "header", true);
  3. get_header($header);
  4. ?>

In this way, when we publish the article, add a custom field header, the value of the custom field is 04ie, after adding, the custom template called is header-04ie.php, at this time we need to create a new header-04ie.php header file to be called as a template.The above call is replaced by the header template file. Of course, the template principle of the sidebar comment page is the same.

Back to Top