在 WordPress 中,如何恢复到主要类别?
我正在使用以下循环,如果所有三个都被选中,那么它只会恢复到最后一项。我想确保它是主要类别。
<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "names"));
foreach ($term_list as $term) {
$name = $term;
} ?>
最佳答案
这不是 native WordPress 功能,而是 Yoast SEO ( see here ) 的功能。
您可以通过以下方式检查主要状态:
<?php
$term_list = wp_get_post_terms($post->ID, 'category', ['fields' => 'all']);
foreach($term_list as $term) {
if( get_post_meta($post->ID, '_yoast_wpseo_primary_category',true) == $term->term_id ) {
// this is a primary category
}
}
?>
如果您使用自定义分类法,请使用meta_key
_yoast_wpseo_primary_CUSTOM_TAXONOMY
相反。
关于php - 如果选择多个类别,是否获取主要类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43114986/