[英]WordPress - Getting Posts by Custom Post Type and Category, with each category automatically encased in a DIV
很抱歉,标题标题过长。 我试图做到准确。
我需要设计一个WordPress查询,该查询将自动从特定的自定义帖子类型中获取帖子,检测每个帖子的分类方式,然后按类别将帖子输出到页面上,每个类别都封装在自己的DIV中。
例如,我有一个名为“地图数据”的自定义帖子类型。 在此自定义帖子类型中,我有一个继承的分类法,我将其命名为“类别”,在该分类法中,有多个类别,“类别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
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.