
本教程详细介绍了如何在wordpress中,通过自定义分类法(taxonomy)对自定义文章类型(custom post type)进行高效筛选。文章将指导您从注册自定义分类法开始,逐步讲解如何在前端展示分类选项,并最终利用`wp_query`结合`tax_query`参数实现精确的文章过滤,确保内容结构清晰、代码示例完整且符合wordpress最佳实践。
在WordPress开发中,自定义文章类型和自定义分类法是构建复杂网站内容结构的基础。当需要根据特定的分类法筛选自定义文章类型时,理解并正确使用WP_Query的tax_query参数至关重要。本文将提供一个全面的指南,帮助开发者实现这一功能。
首先,我们需要在functions.php文件或通过插件注册自定义文章类型及其关联的自定义分类法。以下是一个注册名为“pdf”的自定义文章类型及其“pdf_cat”分类法的示例:
// 注册自定义分类法 'pdf_cat'
function register_pdf_taxonomy() {
$labels = array(
'name' => _x( 'PDF 分类', 'taxonomy general name', 'your-text-domain' ),
'singular_name' => _x( 'PDF 分类', 'taxonomy singular name', 'your-text-domain' ),
'search_items' => __( '搜索 PDF 分类', 'your-text-domain' ),
'all_items' => __( '所有 PDF 分类', 'your-text-domain' ),
'parent_item' => __( '父级 PDF 分类', 'your-text-domain' ),
'parent_item_colon' => __( '父级 PDF 分类:', 'your-text-domain' ),
'edit_item' => __( '编辑 PDF 分类', 'your-text-domain' ),
'update_item' => __( '更新 PDF 分类', 'your-text-domain' ),
'add_new_item' => __( '添加新 PDF 分类', 'your-text-domain' ),
'new_item_name' => __( '新 PDF 分类名称', 'your-text-domain' ),
'menu_name' => __( 'PDF 分类', 'your-text-domain' ),
);
$args = array(
'hierarchical' => true, // 设置为层级结构,类似标签
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'pdf-category' ), // 设置分类法URL别名
);
register_taxonomy( 'pdf_cat', array( 'pdf' ), $args ); // 将 'pdf_cat' 分类法关联到 'pdf' 文章类型
}
add_action( 'init', 'register_pdf_taxonomy' );
// 注册自定义文章类型 'pdf'
function register_pdf_post_type() {
$labels = array(
'name' => _x( 'PDF 文档', 'Post type general name', 'your-text-domain' ),
'singular_name' => _x( 'PDF 文档', 'Post type singular name', 'your-text-domain' ),
'menu_name' => _x( 'PDF 文档', 'Admin Menu text', 'your-text-domain' ),
'name_admin_bar' => _x( 'PDF 文档', 'Add New on Toolbar', 'your-text-domain' ),
'add_new' => __( '添加新文档', 'your-text-domain' ),
'add_new_item' => __( '添加新 PDF 文档', 'your-text-domain' ),
'new_item' => __( '新 PDF 文档', 'your-text-domain' ),
'edit_item' => __( '编辑 PDF 文档', 'your-text-domain' ),
'view_item' => __( '查看 PDF 文档', 'your-text-domain' ),
'all_items' => __( '所有 PDF 文档', 'your-text-domain' ),
'search_items' => __( '搜索 PDF 文档
', 'your-text-domain' ),
'parent_item_colon' => __( '父级 PDF 文档:', 'your-text-domain' ),
'not_found' => __( '未找到 PDF 文档', 'your-text-domain' ),
'not_found_in_trash' => __( '回收站中未找到 PDF 文档', 'your-text-domain' ),
'featured_image' => _x( 'PDF 封面图', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'your-text-domain' ),
'set_featured_image' => _x( '设置封面图', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'your-text-domain' ),
'remove_featured_image' => _x( '移除封面图', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'your-text-domain' ),
'use_featured_image' => _x( '使用封面图', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'your-text-domain' ),
'archives' => _x( 'PDF 文档存档', 'The post type archive label used in n* menus. Default “Post Archives”. Added in 4.4', 'your-text-domain' ),
'insert_into_item' => _x( '插入到 PDF 文档', 'Overrides the “Insert into post”/“Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'your-text-domain' ),
'uploaded_to_this_item' => _x( '上传到此 PDF 文档', 'Overrides the “Uploaded to this post”/“Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'your-text-domain' ),
'filter_items_list' => _x( '过滤 PDF 文档列表', 'Screen reader text for the filter links on the post type list screen. Added in 4.4', 'your-text-domain' ),
'items_list_n*igation' => _x( 'PDF 文档列表导航', 'Screen reader text for the pagination heading on the post type list screen. Added in 4.4', 'your-text-domain' ),
'items_list' => _x( 'PDF 文档列表', 'Screen reader text for the items list heading on the post type list screen. Added in 4.4', 'your-text-domain' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'pdf' ), // 设置文章类型URL别名
'capability_type' => 'post',
'has_archive' => true, // 启用文章类型存档页面
'hierarchical' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array( 'pdf_cat' ), // 关联分类法
);
register_post_type( 'pdf', $args );
}
add_action( 'init', 'register_pdf_post_type' );在前端页面上,您通常会希望提供一个下拉菜单或链接列表,让用户选择特定的分类来筛选文章。以下代码演示了如何获取并显示“pdf_cat”分类法的所有术语(term):
<select onchange="window.location.href=this.value;">
<option value="">所有分类</option>
<?php
$categories = get_terms( array(
'taxonomy' => 'pdf_cat',
'hide_empty' => false, // 显示没有文章的分类
'orderby' => 'name',
'order' => 'ASC',
) );
if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
foreach ( $categories as $category ) {
// 获取分类法的存档链接
$term_link = get_term_link( $category );
if ( ! is_wp_error( $term_link ) ) {
echo '<option value="' . esc_url( $term_link ) . '">' . esc_html( $category->name ) . '</option>';
}
}
}
?>
</select>注意: 上述示例直接生成了分类法的存档链接。当用户点击或选择某个分类时,页面会跳转到该分类的存档页面(例如:yourdomain.com/pdf-category/category-slug/),WordPress会自动处理该页面的文章筛选。如果希望在当前页面通过AJAX或更复杂的逻辑进行筛选,则需要传递分类ID或slug作为参数。
当用户访问某个自定义分类法的存档页面,或者您需要根据URL参数(例如category_id)在自定义模板中筛选文章时,应使用WP_Query结合tax_query参数。tax_query是处理自定义分类法筛选的正确且推荐方式,因为它允许您精确指定分类法、字段和术语。
以下是如何根据当前分类法的ID筛选“pdf”文章类型的示例:
N世界
一分钟搭建会展元宇宙
138
查看详情
<?php
// 获取当前分类法的ID。
// 如果在分类法存档页面,可以使用 get_queried_object()
// 如果ID来自URL参数,请确保进行安全验证和净化
$current_term = get_queried_object(); // 在分类法存档页面获取当前分类对象
$catid = 0; // 默认值
if ( $current_term && is_a( $current_term, 'WP_Term' ) && $current_term->taxonomy === 'pdf_cat' ) {
$catid = $current_term->term_id;
}
// 如果是从URL参数获取,例如 ?pdf_cat_id=X
// if ( isset( $_GET['pdf_cat_id'] ) ) {
// $catid = absint( $_GET['pdf_cat_id'] );
// }
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$pdf_args = array(
'post_type' => 'pdf', // 指定自定义文章类型
'paged' => $paged, // 处理分页
'post_status' => 'publish', // 只获取已发布的文章
'posts_per_page' => 15, // 每页显示15篇文章
);
// 只有当 $catid 有效时才添加 tax_query
if ( $catid > 0 ) {
$pdf_args['tax_query'] = array(
array(
'taxonomy' => 'pdf_cat', // 指定分类法名称
'field' => 'term_id', // 指定匹配字段,可以是 'term_id', 'slug', 'name'
'terms' => $catid, // 要匹配的分类法术语ID或slug数组
),
);
}
$pdf_query = new WP_Query( $pdf_args );
if ( $pdf_query->h*e_posts() ) :
while ( $pdf_query->h*e_posts() ) : $pdf_query->the_post();
// 在这里显示文章内容
?>
<div class="pdf-item">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<p>分类: <?php the_terms( get_the_ID(), 'pdf_cat', '', ', ', '' ); ?></p>
</div>
<?php
endwhile;
// 分页导航
echo paginate_links( array(
'total' => $pdf_query->max_num_pages,
'current' => $paged,
) );
wp_reset_postdata(); // 重置全局 $post 数据
else :
echo '<p>未找到相关 PDF 文档。</p>';
endif;
?>代码解析:
'tax_query' => array(
'relation' => 'AND', // 如果有多个分类法条件,可以指定 AND 或 OR
array(
'taxonomy' => 'pdf_cat',
'field' => 'term_id',
'terms' => array( $catid_1, $catid_2 ), // 匹配多个ID中的任意一个
'operator' => 'IN',
),
array(
'taxonomy' => 'another_taxonomy',
'field' => 'slug',
'terms' => 'some-slug',
),
)通过本文的指导,您应该已经掌握了在WordPress中注册自定义文章类型和分类法,并在前端展示筛选选项,以及最重要的是,使用 WP_Query 和 tax_query 参数精确筛选自定义文章类型的方法。遵循这些最佳实践,将帮助您构建功能强大、性能优异且易于维护的WordPress网站。
以上就是WordPress自定义文章类型与分类法筛选教程的详细内容,更多请关注php中文网其它相关文章!
相关文章:
俄罗斯Yandex搜索引擎入口_Yandex官网免登录一键访问
铁路12306官网网页端快速入口 铁路12306官方首页登录教程
steam官方网页快速访问 steam账号注册全流程
TikTok国际版官网直达_TikTok国际版官网直达进入在线观看
sublime如何优雅地处理行尾空格_sublime自动清理多余空白字符配置
PHP表单数据传递:如何通过隐藏输入字段获取动态ID
响应式图片在网页设计中的正确实现方法
必由学官网入口 必由学教师登录入口
yandex入口引擎手机版 yandex安卓版下载入口
将HTML Canvas内容转换为可上传的图像文件(File对象)
在WordPress中通过REST API获取BasicAuth保护的远程文章
Win11怎么设置开机NumLock亮 Win11修改注册表InitialKeyboardIndicators值
163邮箱网页版入口导航平台 163邮箱网页版登录入口官网导航
Django表单提交验证失败后保持字段值不刷新
Composer的 "licenses" 命令如何帮助你遵守开源协议_检查项目依赖的许可证合规性
谷歌浏览器浏览体验优化_谷歌浏览器新版直连永久可用提示
微博网页版怎么开启两步验证_微博网页版账号安全两步验证设置方法
机器学习中对数变换预测结果的反向还原
qq游戏跨平台入口_qq游戏多设备同步登录
中兴Axon42Ultra怎样在文件App筛图_iPhone中兴Axon42Ultra文件App筛图【图片筛选】
DLsite中文平台入口 DLsite官网内容在线查看
Go语言JSON解析深度指南:动态访问与结构体映射实践
特斯拉自动驾驶房车计划曝光 原型车将于2027年亮相
铁路12306的积分有效期是多久_铁路12306积分有效期说明
Golang如何安装Swagger工具_GoSwagger文档生成环境
ACG动漫视频网入口 ACG动漫*免费正版观看地址
支付宝如何管理隐私设置_支付宝隐私保护的配置技巧
C#中解析不规范的HTML为XML 常见的坑与解决办法
处理动态列数据:J*a ArrayList的正确初始化与字符累加教程
WooCommerce后台产品编辑页:获取分类ID并实现角色权限控制
UC浏览器官网入口2025最新 UC浏览器网页版正式地址
Google翻译怎么语音输入_Google翻译语音输入功能使用与设置方法
html怎么运行外部js文件中的函数_运html外js文件函数法【技巧】
Spring Boot嵌入式服务器与J*a EE:功能支持深度解析
J*aScript实现动态背景色下的文本与按钮颜色自适应调整
J*aScript对象创建方式_J*aScript设计模式应用
正确连接J*aScript到HTML实现可点击图片与自定义事件处理
不同用户不同价格! 索尼开启账户个性化定价测试
单射、满射与双射的关系 一文理清所有逻辑
优化Lar*el Docker镜像:Composer与PHP版本控制策略
免费抖音短视频入口_抖音网页版短视频免费通道
126邮箱账号注册 电脑版登录入口
Win11怎么修改默认浏览器_Windows 11设置Chrome为默认
c++如何实现一个简单的软件渲染器_c++从零开始的3D图形学
Win11怎么安装Linux子系统 Win11 WSL2安装Ubuntu及环境配置指南
c++中为什么推荐使用using替代typedef_c++现代化类型别名
怎样更改Windows系统的默认安装路径_避免C盘爆满的终极设置【技巧】
没有大陆身份证/银行卡如何实名微信? 亲测有效的几种方法分享
Python大型XML文件高效流式解析教程
探索高级语言到C/C++的转译路径:以Go为例及内存管理策略