php - 帖子的 WordPress 术语列表

标签 php mysql wordpress

在 WordPress 中,我正在构建一个索引页面,其中在表格中列出了我博客的所有文章。该表的每一行都有以下结构:帖子标题、属于某个组的术语。例如,如果是关于电影的:

 Blade Runner       | noir, science fiction
 Gone with the Wind | dramatic

要点是:当我有多个术语时,会出现一些奇怪的错误,返回空集或者有时会列出错误的术语。我很困惑,因为我不明白为什么会发生这种情况。这就是我获取数据的方式。

//Loops through the articles in the table wp_posts
foreach($results as $result){
    $metaStr = "";
    $id = $result->ID;
    $query = "
        SELECT name 
        FROM wp_term_relationships 
        INNER JOIN wp_terms
        ON term_id = term_taxonomy_id
        WHERE object_id = $id AND term_taxonomy_id IN (6, 7, 8, 9, 14, 15, 18, 22, 24, 30)
    ";
    $metas = $wpdb->get_results($query);
    $count = 0;
    foreach($metas as $meta){
        if($count > 0){
            $metaStr .= "<br />";
        }
        $metaStr .= $meta->name;
        $count++;
    }
    print $metaStr;
}

有人可以帮忙吗?我究竟做错了什么?有一个更好的方法吗? 干杯

最佳答案

输入以下函数并仅传递帖子 ID,在您的情况下 $result->ID;

function my_get_term_of_post($postId){
$metaStr="";
$term_list = wp_get_post_terms($postId, 'console', array("fields" => "all"));
$i=1;
foreach($term_list as $termSingle){
    if($i==1)
     $metaStr=$termSingle->name;
    else
     $metaStr.= "<br/>".$termSingle->name;
    $i++;
}
return $metaStr;

}

$term_list= my_get_term_of_post($result->ID);

关于php - 帖子的 WordPress 术语列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583907/

相关文章:

php - 使用 SQL 值作为 PHP 变量和 strtotime 查找日期差异

css - 如何在特定宽度后替换冗长句子中的字母

CSS 覆盖 Wordpress(插板主题)

PHP - 使用 do-while 循环的更快方法

javascript - 当子链接处于事件状态时,将事件类添加到父 li

mysql - webmatrix 无法连接到本地主机 :port

css - 将图库移动到 Wordpress 中的页面中心

php - 使用文本字段和复选框查询数据库

javascript - 更改用于加密的主密码

java - prepareStatement for variable table name 的问题