MySQL代码无法显示类别名称(WordPress数据库)

发布于 2024-07-09 12:52:15 字数 653 浏览 11 评论 0原文

为什么此代码无法使用当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2024-07-16 12:52:15

$ra_category 是以下数组:

array(1) {
  [0]=>
  object(stdClass)(1) {
    ["name"]=>
    string(8) "Apples"
  }
}

所以您想要的是:

$ra_category_name = $ra_category[0]->name;

在处理查询结果时,始终使用 var_dump() 检查整个结果,这会有所帮助。

(请注意,您还使用 $ra_category_id 但随后在查询中对值“3”进行硬编码)

$ra_category is the following array:

array(1) {
  [0]=>
  object(stdClass)(1) {
    ["name"]=>
    string(8) "Apples"
  }
}

So what you want is:

$ra_category_name = $ra_category[0]->name;

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文