如何在 wordpress 中为其子项创建类别自定义页面和单个页面
How to create custom page for category and single page for specifed category with its children in wordpress
我想为类别创建自定义页面,并为指定类别及其子项创建单个页面。
首先,为了创建自定义单页我使用了这段代码
if (in_category('blog')){
include (TEMPLATEPATH . '/single-blog.php');
}elseif(in_category('projects')){
include (TEMPLATEPATH . '/single-projects.php');
}
else{
include (TEMPLATEPATH . '/single-default.php');
}
它仅适用于代码中指定的类别,不支持类别的子项。
fo 示例:我想将 single-blog.php
用于其类别为 blog
或 children of blog
的单页帖子。
其次,对于类别页面,我想做与上面针对类别帖子列表所解释的相同的事情。
fo 示例:我想在 category-blog.php
中显示与博客类别或其子类别相关的帖子列表。
我该怎么做。
对于问题的 first 部分,您可能正在寻找 cat_is_ancestor_of
,因此您应该这样写:
function is_ancestor_of( $ancestor_category ) {
$categories = get_the_category(); // array of current post categories
foreach ( $categories as $category ) {
if ( cat_is_ancestor_of( $ancestor_category, $category ) ) return true;
}
return false;
}
$ancestor_category = get_category_by_slug( 'blog' );
if ( in_category( 'blog' ) || is_ancestor_of( $ancestor_category ) ) {
// include
}
对于 second 部分,我知道您想做同样的事情,但要针对存档页面。在那种情况下,它会更简单一些,因为您不会有类别数组:
$archive_category = get_category( get_query_var( 'cat' ) ); // current archive category
$ancestor_category = get_category_by_slug( 'blog' );
if ( is_category( 'blog' ) || cat_is_ancestor_of( $ancestor_category, $archive_category ) {
// include
}
让我知道这是否适合你,
EDIT - 这是一个替代选项(未测试),它不使用——至少不直接使用——foreach
循环。不确定它是否具有更高的性能。
$ancestor_category = get_category_by_slug( 'blog' );
$children_categories = get_categories( array ( 'parent' => $ancestor_category->cat_ID ) ); // use 'child_of' instead of 'parent' to get all descendants, not only children
$categories = get_the_category(); // array of current post categories
if ( in_category( 'blog' ) || ! empty( array_intersect( $categories, $children_categories ) ) {
// include
}
我想为类别创建自定义页面,并为指定类别及其子项创建单个页面。
首先,为了创建自定义单页我使用了这段代码
if (in_category('blog')){
include (TEMPLATEPATH . '/single-blog.php');
}elseif(in_category('projects')){
include (TEMPLATEPATH . '/single-projects.php');
}
else{
include (TEMPLATEPATH . '/single-default.php');
}
它仅适用于代码中指定的类别,不支持类别的子项。
fo 示例:我想将 single-blog.php
用于其类别为 blog
或 children of blog
的单页帖子。
其次,对于类别页面,我想做与上面针对类别帖子列表所解释的相同的事情。
fo 示例:我想在 category-blog.php
中显示与博客类别或其子类别相关的帖子列表。
我该怎么做。
对于问题的 first 部分,您可能正在寻找 cat_is_ancestor_of
,因此您应该这样写:
function is_ancestor_of( $ancestor_category ) {
$categories = get_the_category(); // array of current post categories
foreach ( $categories as $category ) {
if ( cat_is_ancestor_of( $ancestor_category, $category ) ) return true;
}
return false;
}
$ancestor_category = get_category_by_slug( 'blog' );
if ( in_category( 'blog' ) || is_ancestor_of( $ancestor_category ) ) {
// include
}
对于 second 部分,我知道您想做同样的事情,但要针对存档页面。在那种情况下,它会更简单一些,因为您不会有类别数组:
$archive_category = get_category( get_query_var( 'cat' ) ); // current archive category
$ancestor_category = get_category_by_slug( 'blog' );
if ( is_category( 'blog' ) || cat_is_ancestor_of( $ancestor_category, $archive_category ) {
// include
}
让我知道这是否适合你,
EDIT - 这是一个替代选项(未测试),它不使用——至少不直接使用——foreach
循环。不确定它是否具有更高的性能。
$ancestor_category = get_category_by_slug( 'blog' );
$children_categories = get_categories( array ( 'parent' => $ancestor_category->cat_ID ) ); // use 'child_of' instead of 'parent' to get all descendants, not only children
$categories = get_the_category(); // array of current post categories
if ( in_category( 'blog' ) || ! empty( array_intersect( $categories, $children_categories ) ) {
// include
}