当taxonomy is unavailable/unknown.
在我的情况下,当使用get_term_by, 在某些情况下,只有术语Slug(没有术语ID或分类法)。这让我来到了这里。然而,提供的答案并没有完全解决我的问题。
空的解决方案$taxonomy
// We want to find the ID to this slug.
$term_slug = \'foo-bar\';
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
// If term object is returned, break out of loop. (Returns false if there\'s no object)
if ( $term_object = get_term_by( \'slug\', $term_slug , $taxonomy ) ) {
break;
}
}
$term_id = $term_object->name;
echo \'The Term ID is: \' . $term_id . \'<br>\';
var_dump( $term_object );
结果
The Term ID is: 32
object(WP_Term)
public \'term_id\' => int 32
public \'name\' => string \'Example Term\'
public \'slug\' => string \'example-term\'
public \'term_group\' => int 0
public \'term_taxonomy_id\' => int 123
public \'taxonomy\' => string \'category\'
public \'description\' => string \'\'
public \'parent\' => int 0
public \'count\' => int 23
public \'filter\' => string \'raw\'
如下所示,该概念得到
$taxonomies
, 在阵列中循环,如果
get_term_by()
返回一个匹配项,然后它立即跳出foreach循环。
Note: 我试图从术语Slug中搜索相关分类法(ID或Slug),但不幸的是,我在WordPress中找不到任何可用的方法。