0

I'm in trouble with Bootstrap and Wordpress.

I want to get Taxonomy list to expand (show custom posts from Taxonomy). But it is hard!!!!

Its basically a custom sidebar

My code:

<h3>
    Portfólio
</h3>


<?php   

$catprod = get_terms( array(
            'taxonomy' => 'categoria-produto',
            'order'    => 'DESC'
     )); // Todas as categorias


$args = array(
      'post_type' => 'produto'
     );

$prodtype = new WP_Query($args);


?>




   <div class="panel-group">

       <div class="panel-heading">
        <?php foreach( $catprod as $cat ) { ?>
           <h3 class="panel-title">
               <a data-toggle="collapse" href="#<?php echo $cat->slug ?>" class="accordion-toggle" data-parent="#accordion">
                  <?php echo $cat->name ?>
               </a>
           </h3>

           <div id="<?php echo $cat->slug ?>" class="panel-collapse collapse in">
             <div class="panel-body">
                  <?php foreach( $prodtype as $prod ) { ?>
                      <li>
                        <a href="<?php the_permalink() ?>">
                            <?php the_field('titulo') ?>
                        </a>
                      </li>
                  <?php } ?>
             </div>
           </div>
        <?php } ?>  
       </div>  

   </div> 
  • What is the problem exactly? It is hard is hardly an explanation, – Picard Jun 10 '17 at 8:17
  • you have to use WP_Query inside foreach loop and use tax_query as i mentioned in code snippet below – Bikash Waiba Jun 10 '17 at 18:35
0

You can do something like this

<h3>
    Portfólio
</h3>

<?php   

$catprod = get_terms( array(
            'taxonomy'  => 'categoria-produto',
            'order'     => 'DESC',
            'parent'    =>0, 
            'hide_empty' => false,
     )); // Todas as categorias




?>
   <div class="panel-group">

       <div class="panel-heading">
        <?php foreach( $catprod as $cat ): ?>
           <h3 class="panel-title">
               <a data-toggle="collapse" href="#<?php echo $cat->slug ?>" class="accordion-toggle" data-parent="#accordion">
                  <?php echo $cat->name ?>
               </a>
           </h3>
           <?php 

           $args = array(
                'post_type' => 'produto',
                'tax_query' => array(
                        array(
                            'taxonomy' => 'categoria-produto',
                            'terms' => $cat->slug,
                            'field' => 'slug',
                            'include_children' => true,
                            'operator' => 'IN'
                        )
                    ),

             );

            $prodtype = new WP_Query($args);

           ?>
           <div id="<?php echo $cat->slug ?>" class="panel-collapse collapse in">
             <div class="panel-body">
                  <?php while( $query->have_posts() ):$query->the_post(); ?>
                      <li>
                        <a href="<?php the_permalink() ?>">
                            <?php the_title(); ?>
                        </a>
                      </li>
                  <?php endwhile; wp_reset_postdata(); ?>
             </div>
           </div>
        <?php endforeach; ?>  
       </div>  

   </div> 
0
Now it works...

<h3>
    Portfólio
</h3>


<?php   

$catprod = get_terms( array(
            'taxonomy' => 'categoria-produto',
            'order'    => 'DESC'
     )); 


?>




   <div class="panel-group">

       <div class="panel-heading">

        <?php foreach( $catprod as $cat ) { ?>
           <h3 class="panel-title">
               <a data-toggle="collapse" href="#<?php echo $cat->slug ?>" class="accordion-toggle" data-parent="#accordion">
                  <?php echo $cat->name ?>
               </a>
           </h3>

                <!-- expand -->
                   <div id="<?php echo $cat->slug ?>" class="panel-collapse collapse in">
                <?php //foreach( $catprod as $prod ) { ?> 

                        <?php   
                        $args = array(
                            'post_type' => 'produto',
                            //'posts_per_page' => 6,
                            'orderby' => 'title',
                            'order' => 'ASC',
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'categoria-produto',
                                    'field' => 'slug',
                                    'terms' => $cat->slug
                                )
                            )
                        );
                        $content = new WP_Query( $args );       
                        ?>                            

                     <div class="panel-body">
                       <?php while ( $content->have_posts() ) : $content->the_post(); ?>                         
                          <li>
                            <a href="<?php the_permalink() ?>">
                                <?php the_field('titulo') ?>
                            </a>
                          </li>
                       <?php endwhile; ?>
                       <?php wp_reset_query() ?>                                  
                     </div>


                <?php //} ?>
                   </div>
                <!--/expand -->   

        <?php } ?>  


       </div>  

   </div> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.