以下是我正在努力实现的目标:
我有几个类别,每个类别都有几个帖子。现在,在类别上。php我能够输出所有类别的列表,并在该列表中显示当前类别的帖子列表。
汽车(类别:currentcat)现代宝马(BMW)奔驰(Mercedes)建筑师(Architects)常用的名字足球队(Football Team)实际上我输出了每个类别的具体帖子,但通过CSS将它们隐藏起来,只显示currentcat的帖子。在单条上查看单条帖子时。php(例如“Mercedes”),我想要相同的,但对于指定的所有类别:
汽车(类别:currentcat)现代宝马(BMW)梅赛德斯(Mercedes)建筑师(Architects)最受欢迎的名字(类别:currentcat)(请原谅这个极好的例子。)在类别上。我使用的php:
<ul>
<?php
$cargs = array(
\'type\' => \'post\',
\'child_of\' => 0,
\'parent\' => \'\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 0,
\'hierarchical\' => 1,
\'exclude\' => \'1\',
\'include\' => \'\',
\'number\' => \'\',
\'taxonomy\' => \'category\',
\'pad_counts\' => false
);
$categories=get_categories($cargs);
foreach($categories as $category) {
if($cat != $category->term_id) {
echo \'<li class="zk_category-item"><a href="\'.get_category_link($category->cat_ID).\'">\'.$category->cat_name."</a><ul>";
}
else {
echo \'<li class="currentcat"><a href="\'.get_category_link($category->cat_ID).\'" class="activelink">\'.$category->cat_name."</a><ul>";
}
$category_posts=get_posts(\'category=\'.$category->cat_ID);
foreach($category_posts as $post) {
echo \'<li class="zk_project-item"><a href="\'.$post->guid.\'">\'.$post->post_title.\'</a></li>\';
};
echo "</ul></li>";
};
?>
</ul>
我明白为什么这对单曲不起作用了。php,但我不知道如何调整它,使其适用于可能分配了多个类别的单个帖子。
我考虑并尝试在single上获取当前帖子的所有指定类别的数组。php并检查$category->term\\u id是否显示在此数组中,但没有任何效果。
非常感谢您的帮助!
最合适的回答,由SO网友:KingKunta 整理而成
用下面的代码解决了它。我在上面发布的想法是可行的,但我不得不在一个新的数组中重新排列指定的类别。
<ul>
<?php
$cargs = array(
\'type\' => \'post\',
\'child_of\' => 0,
\'parent\' => \'\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 0,
\'hierarchical\' => 1,
\'exclude\' => \'1\',
\'include\' => \'\',
\'number\' => \'\',
\'taxonomy\' => \'category\',
\'pad_counts\' => false
);
$categories = get_categories($cargs);
$post = $wp_query->post;
$postid = $post->ID;
$postcategories = get_the_terms($postid, \'category\');
$postcategoriesids = array();
foreach ($postcategories as $postcategory) {
$postcategoriesids[] = $postcategory->term_id;
};
foreach($categories as $category) {
if(in_array($category->term_id, $postcategoriesids)) {
echo \'<li class="currentcat"><a href="\'.get_category_link($category->cat_ID).\'" class="activelink">\'.$category->cat_name."</a><ul>";
}
else {
echo \'<li class="zk_category-item"><a href="\'.get_category_link($category->cat_ID).\'">\'.$category->cat_name."</a><ul>";
}
$category_posts=get_posts(\'category=\'.$category->cat_ID);
foreach($category_posts as $post) {
echo \'<li class="zk_project-item"><a href="\'.$post->guid.\'">\'.$post->post_title.\'</a></li>\';
};
echo "</ul></li>";
};
?>