从自定义帖子类型列出类别及其帖子 [英] List categories and their posts from custom post type

查看:132
本文介绍了从自定义帖子类型列出类别及其帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示类别及其帖子. (自定义帖子类型)

I want to show the categories and their posts. (custom post type)

它应该看起来像这样:

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

推荐答案

尝试一下:

$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

i just added this: 'term' => $category->slug

这篇关于从自定义帖子类型列出类别及其帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆