Fab*_*bio 2 php mysql wordpress
在Wordpress中,我正在构建一个索引页面,我在表格中列出了我博客的所有文章.该表的每一行都具有以下结构:帖子标题,属于某个组的术语.例如,如果它是关于电影:
Blade Runner | noir, science fiction
Gone with the Wind | dramatic
Run Code Online (Sandbox Code Playgroud)
关键是:当我有多个术语时,会出现一些奇怪的错误,它会返回一个空集,或者有时会列出错误的术语.我很困惑,因为我无法弄清楚为什么会这样.这就是我获取数据的方式.
//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;
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?我究竟做错了什么?有一个更好的方法吗?干杯
小智 5
放下以下函数,只传递post 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;
Run Code Online (Sandbox Code Playgroud)
}
$ term_list = my_get_term_of_post($ result-> ID);
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |