php - 如何将自定义帖子分类名称仅放入数组?
问题描述
我在 wordpress 中有自定义帖子类型。自定义帖子有分类法。
<?php
$customQuery = new WP_Query([
'post_type' => 'custom'
]);
while ($customQuery->have_posts()) {
$customQuery->the_post();
print_r(the_taxonomies());
}
?>
结果:
Array
(
[portfolio_tag] => Taxonomies: <a href="http://localhost/wordpress/custom_tag/app/">App</a> and <a href="http://localhost/wordpress/custom_tag/developer/">Developer</a>.
)
如何让所有分类名称在wordpress中排列?
Array
(
'App',
'Developer'
)
解决方案
您可以使用wp_get_object_terms
或wp_get_post_terms
如果它不起作用,那么您将功能替换wp_get_object_terms
为wp_get_post_terms
in wp_get_object_terms
andwp_get_post_terms
函数有 2 个参数,第一个是 post id,第二个是 custom-taxonomy-name,所以你可以在这里传递第二个参数
为了知识
https://codex.wordpress.org/Function_Reference/wp_get_post_terms
和
https://codex.wordpress.org/Function_Reference/wp_get_object_terms
<?php
$customQuery = new WP_Query([
'post_type' => 'custom'
]);
while ($customQuery->have_posts()) {
$customQuery->the_post();
$arr_get_terms = wp_get_object_terms($customQuery->ID, 'custom-taxonomy-name-here');
print_r($arr_get_terms);
}
?>
推荐阅读
- xaml - 如何在 UWP 中访问用户控件内的样式
- c# - 关于 open Method(MySQL, Connector/Net) 的作用
- json - 如何使用 SWIFT 通过 JSON 将数据从多个视图控制器发布到服务器
- php - 如何将输入字段的 IF 条件放入联系表单(PHP,CODEIGNITER)
- dns - /etc/network/interfaces:4:具有空值的选项
- sql-server-2008 - 图表横轴标签间隔
- vue.js - nuxt 基础组件未在道具绑定图像上显示文件加载器路径
- postgresql - PostgreSQL 数据库同步
- java - 在 Textview 中使用 HTML 标签形成一个从外部文件读取数据的表格
- angular - 如何在角度组件之前捕获已解析的数据?