我有一个WordPress模板,我试图在不同的类别页面上显示不同的内容:
single_cat_title('');
如何在php模板中使用它?当我有一个类别(例如CATEGORY-1
)时,我需要显示与另一个类别不同的内容(例如CATEGORY-2
)。
if single_cat_title(''); is a CATEGORY-1 then display CONTENT-1 else DISPLAY CONTENT-1
答案 0 :(得分:0)
我可以想到两个解决方案。
您可以使用category templates。它们背后的想法是你为每个类别创建一个单独的php文件。您可以对类别ID使用category-1.php
,category-2.php
等名称,或者对类别名称使用category-name.php
,category-other-name.php
等。
为避免在每个文件中编写相同的代码片段,您应该将共享代码放在另一个文件或多个文件中(例如shared.php或更好的header.php,footer.php等)。您可以将共享代码包装在functions中以获得更多控制权。然后在所有类别模板中包含共享文件并调用函数:
include('shared.php'); // including the shared code
show_header(); // our function for displaying header
/*
* do category-specific stuff
*/
show_footer(); // our function for displaying footer
您可以使用单个类别模板(默认情况下为category.php
),检查当前类别是什么,并针对不同类别执行不同的操作。有一个用于检查类别的特殊功能,称为is_category()。它看起来像这样:
//(...) some header code here
if(is_category('some_category')) {
// perform specific actions
}
else if(is_category('other_category')) {
// perform specific actions
}
else {
// some unknown category. perform specific actions
}
// (...) some footer code here