1
对不起,长题目标题。我试图准确。WordPress - 通过自定义帖子类型和类别获取帖子,每个类别自动包含在DIV中
我需要设计一个WordPress查询,该查询将自动从某个自定义帖子类型中获取帖子,检测每个帖子如何分类,然后按类别将其输出到页面上,每个类别都包含在自己的DIV中。
例如,我有一个称为“Map Data”的自定义帖子类型。在这个自定义文章类型中,我有一个我已经命名为“类别”的分类学标准,并在该分类标准中包含了许多类别,“类别#1”,“类别#2”等等。每个类别都有一些帖子。
因此,查询应自定义职位类型中得到的所有类别的清单,然后输出是这样的:
<div id="category-1">
<div class="post">This is a post in Category 1</div>
<div class="post">This is another post in Category 1</div>
</div>
<div id="category-2">
<div class="post">This is a post in Category 1</div>
<div class="post">This is another post in Category 1</div>
</div>
我有以下的代码与WordPress默认分类系统的工作原理但是,我需要重新编写它,或者更新它,以便它可以使用自定义的帖子类型及其分类法。
<?php
$cat_args=array();
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'category__in' => array($category->term_id),
);
$posts=get_posts($args);
if ($posts) {
echo '<div class="cat" id="' . $category->slug.'" name="' . $category->name.'">';
foreach($posts as $post) {
setup_postdata($post);
?>
<?php the_title();?>
<?php the_content();?>
<?php
} // foreach($posts
echo '</div>';
} // if ($posts
} // foreach($categories
?>
如果任何人都可以提供更新的代码供我尝试或作为例子,它将非常感激。
你真的很漂亮已近什么!这是以前提交的答案:http://stackoverflow.com/questions/8643508/how-to-group-articles-by-tags/8645453#8645453 – CookiesForDevo