【发布时间】:2016-07-24 21:45:28
【问题描述】:
我想显示类别及其帖子。 (自定义帖子类型)
应该是这样的:
第 1 类
- 帖子 A(有 1 类)
- 帖子 B(有 1 类)
第 2 类
- 发布 X(有第 2 类)
- 职位 Y(有类别 2)
目前我得到以下输出:
第 1 类
- 帖子 A(在第 1 类中)
- 帖子 B(在第 1 类中)
- 发布 X(在第 2 类中)
- 后 Y(在第 2 类中)
第 2 类
- 帖子 A(在第 1 类中)
- 帖子 B(在第 1 类中)
- 发布 X(在第 2 类中)
- 后 Y(在第 2 类中)
这是我的代码:functions.php
...
register_taxonomy(
'aundo-cat',
'cdh_aundo',
array(
'hierarchical' => true,
'label' => 'Kategorien A und O',
'query_var' => true,
'rewrite' => true
)
);
...
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'cdh_aundo',
array(
'labels' => array(
'name' => __( 'A und O' ),
'singular_name' => __( 'A und O' )
),
'public' => true,
'has_archive' => false,
'menu_icon' => 'dashicons-heart',
'rewrite' => array ('slug' => 'a-und-o-der-unternehmenskommunikation'),
'supports' => array (
'title',
'editor',
'excerpt',
'thumbnail',
'category',
'custom-fields',
'revisions' )
)
);
}
模板文件中的代码:
<?php
$cat_args = array (
'taxonomy' => 'aundo-cat',
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
$cat_query = null;
$args = array (
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'post_type' => 'cdh_aundo',
'taxonomy' => 'aundo-cat'
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
echo "<h3>". $category->name ."</h3>";
echo "<ul>";
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php if( get_field('aundo_tipp_nummer') ): ?>
<div class="">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
</li>
<?php
}
echo "</ul>";
}
wp_reset_postdata();
}
?>
【问题讨论】:
-
欢迎来到 StackOverflow !您有什么问题可以帮助您?
-
谢谢你:)。问题在上面。我得到了带有帖子的类别,但它显示了所有帖子,而不仅仅是特定猫中的帖子。
标签: php wordpress post types taxonomy