WordPress 本身并没有您想要的全球分类法。 WordPress 中的类别与博客文章相关联。
为了实现这一点,我将结合使用令人难以置信的 Advanced Custom Fields (ACF) 插件和修改 functions.php 来为您的网站添加自定义分类并将其应用于您的所有帖子类型。
步骤 1
在functions.php中,制作一个像这样的自定义分类(根据您的需要进行修改):
// add_action registers the taxonomy into wordpress
add_action( 'init', 'setup_my_tax' );
// this function sets up the taxonomy to whatever standards you want
// reference register_taxonomy on codex.wordpress.org
function setup_my_tax()
// first parameter becomes the slug of the tax
register_taxonomy( 'capabilities', array( 'post' ), array(
// labels will determine how it show up in a menu
'labels' => array(
'add_new_item' => 'Add New ',
'all_items' => 'All Capabilities',
'edit_item' => 'Edit Capability',
'menu_name' => 'Capabilities',
'name' => 'Capabilities',
'new_item' => 'New Capability',
'not_found' => 'No Capabilities Found',
'not_found_in_trash' => 'No Capabilities Found in Trash',
'parent' => 'Parent of Capability',
'search_items' => 'Search Capabilities',
'singular_name' => 'Capability',
'view_item' => 'View Capability'
),
// important individual settings from register_taxonomy
'hierarchical' => true,
'public' => true,
'query_var' => true,
'show_admin_column' => true,
'show_ui' => true
));
}
第二步
安装 ACF 后,您将使用 GUI 创建包含此分类的自定义字段集。自定义字段集下方是 rule 选项,它们向您展示如何将自定义位应用于所有实体。
第三步
在page.php 和其他模板中,您可以像这样引用您的分类术语:
// Put the capabilities into an array variable
$capObjects = get_field('capabilities');
// Iterate through the the array of tags and output them
// In WordPress, you have to use the term ID to lookup the term name
foreach ($capObjects as $capObject):
echo '<li class="capabilities-item">';
echo '<a href="' . get_term_link($capObject) . '">' . $capObject->name . '</a> ';
echo '</li>';
endforeach;
您现在可以通过跨越所有类型内容的真实标签来调整您的搜索模板。