Категории списков и их сообщения из пользовательского типа сообщения

Я хочу показать категории и их должности. (пользовательский тип сообщения)

Он должен выглядеть так:

Категория 1

  • Сообщение A (есть у кота 1)
  • Сообщение B (есть кат.1)

Категория 2

  • Сообщение X (имеет cat 2)
  • Сообщение Y (имеет кошку 2)

На данный момент я получаю следующий вывод:

Категория 1

  • Сообщение A (в кат.1)
  • Сообщение B (в кат.1)
  • Сообщение X (в кат. 2)
  • Сообщение Y (в кат. 2)

Категория 2

  • Сообщение A (в кат.1)
  • Сообщение B (в кат.1)
  • Сообщение X (в кат. 2)
  • Сообщение Y (в кат. 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' ) ) ); } 

Код в файле шаблона:

 <?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(); } ?> 

Related of "Категории списков и их сообщения из пользовательского типа сообщения"

попробуй это:

 $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(); } 

Я просто добавил это: 'term' => $category->slug

Вы можете попробовать таким образом

 <?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>'; } } ?> 

введите описание изображения здесь