很抱歉的长期问题的标题。 我试图准确。
我需要设计一个WordPress的查询,将自动从某个自定义后类型职位,检测每一个职位是如何进行分类,然后输出的职位,按类别到页面上,每个类别中自己的DIV包裹。
例如,我有一个自定义后类型,称为“地图数据”。 在这个自定义后的类型,我有我有一个名为“类别”一heirarchial分类,并且这种分类,一些类别的范围内,“类别#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
?>
如果有人可以提供更新的代码让我试试,或者工作的例子,这将是非常赞赏。
我这样做是得到所有分类,但你的努力很容易就会被修改为主动
// for a given post type, return all
$post_type = 'shows';
$tax = 'show-topic';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC', 'exclude' => '135, 49, 25, 24, 54'));
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => - 1,
'orderby' => 'title',
'order' => 'ASC',
'caller_get_posts' => 1
); // END $args
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
echo '<h3>' . $tax_term->name . '</h3>';
while ($my_query->have_posts()) : $my_query->the_post();
?>
<div class="post row" id="post-<?php the_ID(); ?>">
<div class="thumb-box three column">
<?php
$src = wp_get_attachment_image_src(get_post_thumbnail_id());
if (has_post_thumbnail()) {
the_post_thumbnail();
} else {
if (get_post_meta($post->ID, "thumbnail", true)):
?>
<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
<?php else: ?>
<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/insp-tv-small.png" alt="<?php the_title(); ?>" /></a>
<?php endif;
}
?>
</div>
<div class="post-content nine columns">
<h4 class="posttitle archiveposttitle">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'buddypress') ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h4>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php
endwhile;
} // END if have_posts loop
wp_reset_query();
} // END foreach $tax_terms
} // END if $tax_terms
?>
文章来源: WordPress - Getting Posts by Custom Post Type and Category, with each category automatically encased in a DIV