Bootstrap 4 Accordion中具有自定义分类的自定义帖子类型

时间:2017-08-11 作者:Ronaldo Chevalier

我有一个带有自定义分类法的CPT,我需要在Bootstrap 4手风琴中显示此内容。

到目前为止,我有:

<?php 
$terms = get_terms( array(
            \'taxonomy\' => \'ano\'
        ));

foreach($terms as $term) { ?>    <div id="accordion" role="tablist">      <div class="card">
<div class="card-header" role="tab" id="heading-<?php the_ID(); ?>">
  <h5 class="mb-0">
    <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" aria-expanded="true" aria-controls="collapse-<?php the_ID(); ?>">
      <?php echo $term->name;  ?>
    </a>
  </h5>
</div>

<div id="collapse-<?php the_ID(); ?>" class="collapse<?php echo ($the_query->current_post == 0 ? \' in\' : \'\'); ?> show" role="tabpanel" aria-labelledby="heading-<?php the_ID(); ?>" data-parent="#accordion">
  <div class="card-body">
    <?php   $event = new WP_Query(\'post_type=Paradas&posts_per_page=-1\');
            while ($event->have_posts()) : $event->the_post(); ?>                           
            <p><?php the_title(); ?></p>
            <?php endwhile ; wp_reset_query(); ?>    

标题中正确显示了分类法,但内容相同,与分类法无关

1 个回复
SO网友:Ronaldo Chevalier

解决方案如下:

<?php
$post_type = \'paradas\';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( \'post_type\' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :

    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy,
            array(
                \'orderby\'   => \'name\',
                \'order\'     => \'ASC\',
                \'hide_empty\'    => \'1\'
        )
    );

        foreach( $terms as $term ) : 
            // WP_Query arguments
        $args = array (
            \'post_type\'   => $post_type,
            \'posts_per_page\'  => \'-1\',
            \'tax_query\'       => array(
                        array(
                            /**
                 * For get a specific taxanomy use
                 *\'taxonomy\' => \'category\',
                 */
                            \'taxonomy\' => $taxonomy,
                            \'field\'    => \'slug\',
                            \'terms\'    => $term->slug,
                        )
                    )
        );
        // The Query
        $posts = new WP_Query( $args );

        // The Loop
        if( $posts->have_posts() ) : ?>
            <dl id="box-loop-list-<?php echo $term->slug ;?>">    <div id="accordion" role="tablist">    <div class="card">
<div class="card-header" role="tab" id="heading-<?php the_ID(); ?>">
  <h5 class="mb-0">
    <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" aria-expanded="true" aria-controls="collapse-<?php the_ID(); ?>">
      <?php echo $term->name;  ?>
    </a>
  </h5>
</div>

<div id="collapse-<?php the_ID(); ?>" class="collapse<?php echo ($the_query->current_post == 0 ? \' in\' : \'\'); ?>" role="tabpanel" aria-labelledby="heading-<?php the_ID(); ?>" data-parent="#accordion">
  <div class="card-body">

            <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
                <p><?php the_title(); ?></p>
                <?php endwhile; ?>
        </div>
</div>

结束

相关推荐

如何创建4列和2列Twitter-Bootstrap相结合的WordPress循环?

我想根据4列(桌面视图)、2列(mobile view 768px)和1列(mobile view 425px)的组合显示帖子。我在下面找到了很棒的代码:<?php $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1; $args=array( \'post_type\' => \'post\',