这是一个解决方案,用于输出包含在页面上共享当前标签一次的帖子的类别列表,而不是在循环内。我只在标记页面上测试它,其中'标记'查询变量可用。它使用自定义查询using the $wpdb
class。 (这对学习非常有用)。
您需要在开头定义自己的表格前缀,或设置为''
。
测试 我测试了这个代码,直接把它放在tag.php中。将它放在你自己的插件或主题的functions.php中会更聪明。一些调整可能是必要的,以确保$ wp_query对象在该函数内可用。
术语和分类标准 请记住,标签和类别在wordpress数据库中被认为是相等的。与帖子,页面,修订和草稿一样,所有“帖子”,类别和标签都是“条款”。它们之间的区别在于它们的“分类法”,这就是该术语是“类别”还是“标签”。
“条款”表包含所有标签和类别。 'term_taxonomy'存储该术语是标签,类别还是帖子链接。 'term_relationships'表对帖子和分类进行配对。 'object_id'列可以保存该表中的发帖ID或术语ID。
代码大纲
- 设置表前缀
- 获取从query_vars
- 从术语表
- 获取标签的term_id标签名称获取的帖子ID的(命名为“OBJECT_ID ')从term_relationships表中作为数字索引数组
- 将该数值数组转换为以逗号分隔的字符串
分
- 饰件后面的逗号
- 选择具有从#4的帖子ID的从term_relationships作为多维关联数组
- 创建所有的职位具有该标签的术语的阵列中的所有term_taxonomy_id的(类别和标记) 。
- Foreach元素在这个数组中,选择分类法(无论是'category'还是'tag'),如果它是一个类别,则将term_id放入数组中。
- 将该数组转换为字符串以供下一步使用。
- 呼叫
wp_list_categories()
与category_id字符串作为要包括的类别。
代码示例
//define table prefix
$wp_pre = 'wp_';
//get tag name from url
$tag_name = $wp_query->get('tag');
//get term_id from database
$term_id = $wpdb->get_var("
SELECT term_id
FROM ".$wp_pre."terms
WHERE name ='".$tag_name."'");
//select post id's with this tag
$posts_with_tag = $wpdb->get_results("
SELECT object_id
FROM ".$wp_pre."term_relationships
WHERE term_taxonomy_id = '".$term_id."'",
ARRAY_N);
//make string out of returned array in an array
$posts_with_tag_as_string = implode(',',$posts_with_tag[0]);
//trim trailing comma
$posts_with_tag_as_string = rtrim($posts_with_tag_as_string,',');
//select all terms having post id
$terms_having_post = $wpdb->get_results("
SELECT term_taxonomy_id
FROM ".$wp_pre."term_relationships
WHERE object_id
IN ('".$posts_with_tag_as_string."')",
ARRAY_A);
foreach($terms_having_post as $key=>$val){
$post_terms[] = $val['term_taxonomy_id'];
}
//get taxonomy name for each term_having_post
foreach($post_terms as $term_id){
$taxonomy = $wpdb->get_var("
SELECT taxonomy
FROM ".$wp_pre."term_taxonomy
WHERE term_id = '".$term_id."'");
if($taxonomy == 'category'){
$cats[] = $term_id;
}
}
$category_ids = implode(',',$cats);
//trim trailing comma
$category_ids = rtrim($category_ids,',');
//output list of categories with the included id's.
wp_list_categories('include='.$category_ids);
我希望这对你的作品。
UPDATE 在回答您的意见,以get categories和说明等猫元数据,你可以用get_categories
替代wp_list_categories
,同时还传递include='.$category_ids
。这将返回一系列类别对象,您可以根据需要循环显示类别对象,说明,帖子数量等。
新的代码示例(从抄本页面)
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
这将是一个自定义查询。由于标签和类别只与它们共享的帖子间接相关,因此您必须循环所有帖子(或创建适当的SQL连接)才能找到所有组合。你设想什么样的用户界面? – 2010-11-09 11:03:39
嗨,我添加了我的HP循环的外观,我试图得到的标签页循环应该也是类似的,显然只输出标签所在的类别。 – Blackbird 2010-11-09 11:26:28
想想如果通过查询meta的更简单的方法键“is_front_page”并检查标签是否存在于任何这些帖子中?我不知道这一切是否可能。 – Blackbird 2010-11-09 11:28:03