显示相应自定义帖子类型帖子的类别名称

时间:2016-03-28 21:36:56

标签: wordpress foreach custom-post-type custom-taxonomy

您好我有一个自定义帖子类型,它有一些分类法,我已经完成以下代码循环自定义帖子类型中的帖子:

<?php
global $wp_query;                   
$value = strtolower(get_the_title($post));

$wp_query = new WP_Query( array(
    'posts_per_page' => 6,
    'post_type' => array( 'howitworkspt' ),
    'order'   => 'DESC'
) ); ?>

<div class='questionContainerWrapper'>               
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
        <article>
            <div class='teamsShortCode question-container'>
                <h2 class='question'>
                    <span>+</span>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                </h2>
                <div class='answer'><?php the_content(); ?></div>
            </div>
        </article>
    <?php endwhile; ?>

</div>

这导致:

<div class="questionContainerWrapper"> 
    <article>
        <div class="teamsShortCode question-container">
            <h2 class="question">
                <span>+</span>
                <a href="title link">question for category 1</>
            </h2>
            <div class="answer">answer here</div>
        </div>
    </article> 

    <article>
        <div class="teamsShortCode question-container">
            <h2 class="question">
                <span>+</span>
                <a href="title link">question2 for category 1</>
            </h2>
            <div class="answer">answer here</div>
        </div>
    </article> 

    <article>
        <div class="teamsShortCode question-container">
            <h2 class="question">
                <span>+</span>
                <a href="title link">question for category 2</>
            </h2>
            <div class="answer">answer here</div>
        </div>
    </article>                                        
</div>

但我想要的是:

<div class="questionContainerWrapper"> 
    <h2>category1/taxonomy term name here</h2>
    <article>
        <div class="teamsShortCode question-container">
            <h2 class="question">
                <span>+</span>
                <a href="title link">question for category 1</>
            </h2>
            <div class="answer">answer here</div>
        </div>
    </article> 

    <article>
        <div class="teamsShortCode question-container">
            <h2 class="question">
                <span>+</span>
                <a href="title link">question2 for category 1</>
            </h2>
            <div class="answer">answer here</div>
        </div>
    </article> 
</div>

<div class="questionContainerWrapper"> 
    <h2>category2/taxonomy term name here</h2>
    <article>
        <div class="teamsShortCode question-container">
            <h2 class="question">
                <span>+</span>
                <a href="title link">question for category 2</>
            </h2>
            <div class="answer">answer here</div>
        </div>
    </article>                                        
</div>

我尝试过这样做:Get category name with these post by custom post type但无论类别如何,它都会获得相同的帖子,我想获得与该类别相关的帖子。

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php $terms = get_terms('howitworkspt_category', 'hide_empty=1'); ?>
<?php foreach ($terms as $k => $term): ?>
    <div class='questionContainerWrapper'>    
        <h2>
            <?php echo $term->name; ?>
        </h2>
        <?php 
        $params_arr = array(
            'paged' => 1,
            'posts_per_page' => 20,
            'post_type' => 'howitworkspt',
        );
        $params_arr['tax_query'] = array(
            array(
                'taxonomy' => 'howitworkspt_category',
                'field'    => 'slug',
                'terms'    => array( $term->name ),
            )
        );
        $wpq = new WP_Query( $params_arr ); 
        ?>   
        <?php while ( $wpq->have_posts() ) : $wpq->the_post(); ?>
            <article>
                <div class='teamsShortCode question-container'>
                    <h2 class='question'>
                        <span>+</span>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                    </h2>
                    <div class='answer'><?php the_content(); ?></div>
                </div>
            </article>
        <?php endwhile; ?>
    </div>
<?php endforeach ?>

注意:确保将'howitworkspt_category'替换为正确的分类