admin管理员组

文章数量:1122832

I have a CPT with a Custom Taxonomy and a I need to show this content in the Bootstrap 4 Accordion.

So far, i have this:

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


The taxonomy is being shown correctly in the header but the content is being the same, independent of the taxonomy

I have a CPT with a Custom Taxonomy and a I need to show this content in the Bootstrap 4 Accordion.

So far, i have this:

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


The taxonomy is being shown correctly in the header but the content is being the same, independent of the taxonomy

Share Improve this question edited Aug 11, 2017 at 22:23 Ronaldo Chevalier asked Aug 11, 2017 at 22:14 Ronaldo ChevalierRonaldo Chevalier 12 bronze badges 2
  • you're not setting any taxonomy parameters in the query, just post_type and posts_per_page. – Milo Commented Aug 12, 2017 at 0:05
  • Milo, I put the taxonomy before the accordion.. I test a bunch of codes and finally figure out how to do.. – Ronaldo Chevalier Commented Aug 14, 2017 at 17:34
Add a comment  | 

1 Answer 1

Reset to default 0

Here the solution:

<?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>

本文标签: Custom Post Type with Custom Taxonomy in Bootstrap 4 Accordion