如何在Wordpress中显示公文包

如何在Wordpress中显示公文包,wordpress,Wordpress,基本上,我试图在WordPress中实现这一点 我可以用自定义的帖子类型和分类法显示所有内容,但是当我尝试创建分类法类别的过滤器时,我认为我遗漏了一些东西,因此,我只能在没有过滤器的情况下显示公文包。下面是我的代码,我肯定遗漏了什么,但不确定是什么 /**Case Study Custom Post Type**/ function casestudy_posttype() { $args = array( 'public' => true,

基本上,我试图在WordPress中实现这一点

我可以用自定义的帖子类型和分类法显示所有内容,但是当我尝试创建分类法类别的过滤器时,我认为我遗漏了一些东西,因此,我只能在没有过滤器的情况下显示公文包。下面是我的代码,我肯定遗漏了什么,但不确定是什么

    /**Case Study Custom Post Type**/ 
function casestudy_posttype() {
    $args = array(
        'public'    => true,
        'label'     => ( 'Case Study'),
        'menu_icon' => 'dashicons-category',
        "supports" => [ "title", "editor", "thumbnail" ],
    );
    register_post_type( 'casestudy', $args );
}
add_action( 'init', 'casestudy_posttype' );



add_action( 'init', 'casestudy_cates' );

function casestudy_cates() {
    register_taxonomy(
        'casestudy-cates',
        'casestudy',
        array(
            'label' => ( 'Case Study Categories' ),
            'rewrite' => array( 'slug' => 'casestudy-category' ),
            'hierarchical' => true,
        )
    );
}

/**END CUSTOM POST TYPE FOR case study **/
在页面上显示

<!-- case_study_area  -->
<div class="case_study_area case_page">
    <div class="container">
        <div class="row">
            <div class="col-xl-12">
                <div class="portfolio-menu text-center">

                    <?php global $post;
    $taxonomy = 'casestudy-cates';
    $terms = wp_get_post_terms($post->ID, $taxonomy);

    if ($terms && !is_wp_error($terms)) :?>

                    <button class="active" data-filter="*">All</button>
                        <?php
                foreach ($terms as $serv_terms) :
                    ?>
                        <li class="tab-<?php echo $count ?>"><?php echo $serv_terms->name; ?>
                        </li>
                        <button data-filter="<?php echo $serv_terms->slug; ?>"><?php echo $serv_terms->name; ?></button>
                        <?php
                endforeach ?>


                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="row grid">
            <?php
        $args = array('post_type' => 'casestudy', 'order' => 'ASC');
        query_posts($args);
        while (have_posts()) : the_post();
            global $post;
            ?>
            <div class="col-xl-4 grid-item">
                <div class="single_case">
                    <div class="case_thumb">
                        <?php the_post_thumbnail()?>
                    </div>
                    <div class="case_heading">

                        <?php
                        global $post;
                        $taxonomy = 'casestudy-cates';
                        $terms = wp_get_post_terms($post->ID, $taxonomy);
                        if ($terms && !is_wp_error($terms)) :?>
                        <?php foreach ($terms as $serv_terms) : ?>
                        <span><?php echo $serv_terms->name; ?></span>
                        <?php endforeach ?>
                        <?php endif; ?>
                        <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
                    </div>
                </div>
            </div>
            <?php endwhile; wp_reset_query(); ?>

            <?php foreach ($terms as $key=>$term) :?>
            <?php

                $args = array('post_type' => 'casestudy', 'order' => 'ASC', 'tax_query' => array(
                    array(
                        'taxonomy' => 'casestudy_cates', 'field' => 'slug', 'terms' => $term->slug)
                ));
                query_posts($args);
                while (have_posts()) : the_post();
    ?>
            <div
                class="col-xl-4 grid-item <?php echo $term->slug;?>">
                <div class="single_case">
                    <div class="case_thumb">
                        <?php the_post_thumbnail()?>
                    </div>
                    <div class="case_heading">

                        <?php
                        global $post;
                        $taxonomy = 'casestudy-cates';
                        $terms = wp_get_post_terms($post->ID, $taxonomy);
                        if ($terms && !is_wp_error($terms)) :?>
                        <?php foreach ($terms as $serv_terms) : ?>
                        <span><?php echo $serv_terms->name; ?></span>
                        <?php endforeach ?>
                        <?php endif; ?>
                        <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
                    </div>
                </div>
            </div>
            <?php endwhile; wp_reset_query();?>
            <?php endforeach;?>
        </div>

    </div>
</div>
<!-- /case_study_area  -->

全部的

问题是
$terms=wp\u get\u post\u terms($post->ID,$taxonomy)。这是错误的函数。也就是说,要检索特定文章的分类术语,您需要获取该分类的所有术语。您需要使用
get_terms
-@disifor,谢谢我已经解决了这个问题