问题
I want to show the categories and their posts. (custom post type)
It should look like this:
Category 1
- Post A (has cat. 1)
- Post B (has cat. 1)
Category 2
- Post X (has cat. 2)
- Post Y (has cat. 2)
At the moment I get following output:
Category 1
- Post A (in cat. 1)
- Post B (in cat. 1)
- Post X (in cat. 2)
- Post Y (in cat. 2)
Category 2
- Post A (in cat. 1)
- Post B (in cat. 1)
- Post X (in cat. 2)
- Post Y (in cat. 2)
Heres my code: 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' )
)
);
}
Code in template file:
<?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();
}
?>
回答1:
try this:
$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',
'term' => $category->slug
);
$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();
}
i just added this: 'term' => $category->slug
回答2:
You can try by this way
<?php
$cat = get_terms('category');
foreach ($cat as $catVal) {
echo '<h2>'.$catVal->name.'</h2>';
$postArg = array('post_type'=>'post','posts_per_page'=>-1,'order'=>'desc',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $catVal->term_id
)
));
$getPost = new wp_query($postArg);
global $post;
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo '</ul>';
}
}
?>
来源:https://stackoverflow.com/questions/36418102/list-categories-and-their-posts-from-custom-post-type