我正在寻找的解决方案是,如果某个特定的分类法项目与其他分类法项目勾选,则将其作为默认/优先级
你真的在找这样的东西吗:
$default_slug = \'taxonomy_z\'; // slug of the default term
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$list = wp_list_filter( $terms, array( \'slug\' => $default_slug ) );
if ( ! empty( $list ) ) { // the post **is** in the default term
// If two or more terms assigned to the post, use the default term\'s slug.
$taxonomy_slug = ( count( $terms ) > 1 ) ? $default_slug : $terms[0]->slug;
} else { // the post is **not** in the default term
$taxonomy_slug = $terms[0]->slug;
}
} else { // the post is not in any of the taxonomy\'s terms; use the default term.
$taxonomy_slug = $default_slug;
}
将在代码中替换此部分:
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'taxonomy_z\';
更新
如果分类法“nashville”成为父分类法,而其邻域“hillsboro”和“greenhills”成为其子分类法,是否也有办法将“nashville”作为其默认url slug?
是的,通过将父项(其slug)存储在数组中($default_slugs
在下面的示例中)。然而,这意味着您的帖子不应该有两个父项,它们位于该数组中。一、 e.可以在tennessee
或nashville
, 但不是两者都有。因为否则,permalink slug可能不是您所期望的那样。
$default_slugs = array( \'tennessee\', \'nashville\' ); // list of parent terms/slugs
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$slugs = wp_list_pluck( $terms, \'slug\' );
$list = array_intersect( $slugs, $default_slugs );
if ( ! empty( $list ) ) { // the post **is** in a parent term
// If two or more terms assigned to the post, use a parent term\'s slug.
$default_slug = array_shift( $list );
$taxonomy_slug = ( count( $terms ) > 1 ) ? $default_slug : $terms[0]->slug;
} else { // the post is **not** in a parent term
$taxonomy_slug = $terms[0]->slug;
}
} else { // the post is not in any of the taxonomy\'s terms; use a default term.
$taxonomy_slug = \'tennessee\';
}