1.如何显示所有自定义帖子类型,并在上面设置一个过滤器,其中的类别可用作过滤器选项卡?
2.如何循环在自定义模板通过自定义分类类别和显示链接?
HTML结构
自定义帖子类型URL:/wp管理/编辑标签。php?分类法=类别
PHP
<?php get_term( $term, $taxonomy, $output, $filter ) ?>
<?php
$args=array(
'name' => 'categories'
);
$output = 'products'; // or names
$taxonomies=get_taxonomies($args,$output);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
echo '<p>' . $taxonomy->name . '</p>';
}
}
?>
<?php
$args = array(
'public' => true,
'_builtin' => false
);
$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies( $args, $output, $operator );
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
echo '<p>' . $taxonomy . '</p>';
}
}
您可以这样做来获取自定义分类法的所有术语:
https://developer.wordpress.org/reference/functions/get_terms/
$terms = get_terms( array(
'taxonomy' => 'categories',
'hide_empty' => false,
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
}
返回具有以下结构的数组:
array(
[0] => WP_Term Object
(
[term_id] =>
[name] =>
[slug] =>
[term_group] =>
[term_taxonomy_id] =>
[taxonomy] =>
[description] =>
[parent] =>
[count] =>
[filter] =>
)
$term_link
将为您提供分类学术语归档的永久链接。
https://developer.wordpress.org/reference/functions/get_term_link/
关于如何实现过滤器选项卡的其他问题:请查看此插件:https://wordpress.org/plugins/beautiful-taxonomy-filters/
要查找与给定文章类型相关联的分类法,请使用WordPressget_object_taxonomies()
函数,如下所示:
$taxonomies = get_object_taxonomies('post', 'objects');
$分类法
将是一个由WP_Taxonomy
对象组成的数组。省略第二个参数以获得分类学段的数组。
要按自定义分类法进行查询,请创建一个新的WP_查询,如下所示:
$args = [
'posts_per_page' => -1,
'tax_query' => [
[
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => ['your-term'],
],
],
];
$filterQuery = new WP_Query($args);
虽然分类学关联可以使用register_post_type
在post_types上声明,但该关联是可选的,非常弱。在内部,WordPress走另一条路,将post_types分配给分类法。
每个分类法都有一个object_type
属性,该属性是它所知道的post_types的段元组。深入研究register_post_type
源代码,我们看到它为分类法
参数属性中的每个项目调用register_taxonomy_for_object_type
,然后简单地将一个段塞添加到Taxonomy的object_type
数组。这是唯一一次使用post_type的分类法属性。我更喜欢在注册分类法时声明post_types,因为它更接近WordPress的工作方式,并且误解了这种关联在过去给我带来了很多痛苦。