Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查当前存档页面类别是否为特定父级的子类别_Php_Wordpress - Fatal编程技术网

Php 检查当前存档页面类别是否为特定父级的子类别

Php 检查当前存档页面类别是否为特定父级的子类别,php,wordpress,Php,Wordpress,如何检查当前存档页是否为父类别的子类别 假设我们有一个名为“经济”的父类别,它有3个不同的子类别。如果我在其中一个子类别的归档页面上,我希望实现以下逻辑: If (current archive page is a subcategory of the parent "Economy") { //do something } 如何获得此结果?您可以使用get\u queryed\u object()获取当前页面的类别,然后从中获取父类别id。然后,您可以将该id与正在查找的

如何检查当前存档页是否为父类别的子类别

假设我们有一个名为“经济”的父类别,它有3个不同的子类别。如果我在其中一个子类别的归档页面上,我希望实现以下逻辑:

If (current archive page is a subcategory of the parent "Economy") {
//do something
}

如何获得此结果?

您可以使用
get\u queryed\u object()
获取当前页面的类别,然后从中获取父类别id。然后,您可以将该id与正在查找的id进行比较,或者使用该id获取要比较的父类别的slug或名称

1。按ID进行比较

最短的代码是比较父类别的id。(您可以在管理中看到“经济”类别的id)

2。按段塞比较

如果要将其与特定的父slug进行比较,则需要首先获取父类别对象,然后检查其slug

$archive_cat = get_queried_object();   // get category for this archive page

// get the parent category object using the parent id
$category_parent = get_term( $archive_cat->category_parent, 'category' );

// Check the slug of category's parent
if ($category_parent->slug == "ecomony") {
    //do something
}
3。按名称进行比较

您通常会在比较中使用id或slug,因为它们是字母数字的,因此更容易匹配。名称可以包含空格、标点、大小写等,这会使比较复杂化

但是,如果您有简单的名称,并且希望在比较中使用这些名称,则可以使用与slug类似的方法:

$archive_cat = get_queried_object();   // get category for this archive page

// get the parent category object using the parent id
$category_parent = get_term( $archive_cat->category_parent, 'category' );

// Check the name of category's parent
if ($category_parent->name== "Ecomony") {
    //do something
}

您可以使用
get\u category\u parents()
并检查这是否等于“Economy”
$archive_cat = get_queried_object();   // get category for this archive page

// get the parent category object using the parent id
$category_parent = get_term( $archive_cat->category_parent, 'category' );

// Check the name of category's parent
if ($category_parent->name== "Ecomony") {
    //do something
}