get_category_by_slug()
返回类别对象或FALSE
:
$cat1 = get_category_by_slug( \'cata\' );
print \'<pre>\' . htmlspecialchars( print_r( $cat1, TRUE ) ) . \'</pre>\';
结果:
stdClass Object
(
[term_id] => 3
[name] => Cat A
[slug] => cata
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 2
[cat_ID] => 3
[category_count] => 2
[category_description] =>
[cat_name] => Cat A
[category_nicename] => cata
[category_parent] => 0
)
因此,要获得一个ID数组,只需检查是否为每个slug获得了一个对象,然后添加
term_id
到阵列:
$category__not_in = array();
$slugs = array ( \'cata\', \'catdoesnotexist\' );
foreach ( $slugs as $slug )
{
$cat = get_category_by_slug( $slug );
$cat and $category__not_in[] = $cat->term_id;
}
$args = array (
\'category__not_in\' => $category__not_in
);
与其硬编码脚本中的段塞,不如考虑添加
term meta data 到类别。您将需要一个额外的查询来查找所有需要的术语,但您的脚本仍然可以重用。