MySQL代码无法显示类别名称(WordPress数据库)
为什么此代码无法使用当前的 WordPress 分类系统显示类别名称“Apples”? 类别名称存储在 $wpdb->terms 表 (wp_terms) 中。
<?php
$ra_category_id = 3;
$ra_category = $wpdb->get_results("SELECT name FROM $wpdb->terms WHERE term_id = '3'");
$ra_category_name = $ra_category->name;
?>
<h3>Category: <?php echo $ra_category_name; ?></h3>
表行是
term_id name slug term_group
1 Uncategorized uncategorized 0
2 Blogroll blogroll 0
3 Apples apples 0
4 Bananas bananas 0
Why does this code fail to display the category name "Apples" using the current WordPress taxonomy system? The Category names are stored in the $wpdb->terms table (wp_terms).
<?php
$ra_category_id = 3;
$ra_category = $wpdb->get_results("SELECT name FROM $wpdb->terms WHERE term_id = '3'");
$ra_category_name = $ra_category->name;
?>
<h3>Category: <?php echo $ra_category_name; ?></h3>
The table rows are
term_id name slug term_group
1 Uncategorized uncategorized 0
2 Blogroll blogroll 0
3 Apples apples 0
4 Bananas bananas 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ra_category 是以下数组:
所以您想要的是:
在处理查询结果时,始终使用
var_dump()
检查整个结果,这会有所帮助。(请注意,您还使用
$ra_category_id
但随后在查询中对值“3”进行硬编码)$ra_category is the following array:
So what you want is:
When dealing with query results, always check the whole result with a
var_dump()
, it helps.(note that you're also using
$ra_category_id
but then hardcoding the value "3" in your query)