Create a loop for each post category

Asked

Viewed 159 times

0

Hi I am new to programming and new to wordpress development.

I’m trying to create a testimonial page, with custom post typos atlizando of sub categories. The idea is to have 4 categories of testimonials. And have a loop for each category. For this I am using the accordion of bootstrap 4. Then, when the user clicks on a certain category, the post loop of that category is displayed.

I already managed to foreach to display the menu of the 4 categories. But I’m not managing to structure the lopps within each category.

Follows the code:

    <!-- Display the menu of categories, essa parte funciona OK -->
    <?php   
    $catList = get_categories(
    array(
    'child_of' => 103,
    'orderby' => 'id',
    'order' => 'ASC',
    'hide_empty' => '0'
    ) );
    ?>
    <div class="accordion" id="accordionExample">
    <?php $c = 1 ?>
    <?php foreach($catList as $catItem) : ?>
    <div class="card">
    <div class="card-header" id="<?php echo $catItem->slug?>-tab">
    <h5 class="mb-0">
    <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#<?php echo $catItem->slug?>" aria-expanded="<?php echo ($c == 2)?'true':'false'; ?>" aria-controls="<?php echo $catItem->slug?>">
    <?php echo $catItem->name?>
    </button>
    </h5>
    </div>
    </div>
    <?php $c++ ?>
    <?php endforeach ?>
    <!-- End of Display the menu of categories -->

The propblema is the time to make the loops of each category I’m doing like this:

    <!-- Aqui o loop para cada categoria, Essa parte nao funciona  -->
    <?php $c = 1 ?>
    <?php foreach($catList as $catItem) : ?>
    <?php
    $argsPosts = array(
    'post_type' => 'depoimentos',
    'posts_per_page' => 10,
    'post__not_in' => get_option( 'sticky_posts' ),
    'cat' => $catItem->cat_ID
    ); 
    ?>

    <?php $bladeQuery = new WP_Query($argsPosts); ?><?php if($bladeQuery->have_posts()) : ?><?php while ($bladeQuery->have_posts()) : ?><?php $bladeQuery->the_post(); ?>
    <div id="<?php echo $catItem->slug?>" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
    <div class="card-body">
    HEAR THE CONTENT OF EACH TESTIMONIALS
    </div>
    </div>
    </div>
    <?php endwhile; ?><?php endif; ?><?php wp_reset_postdata(); ?><!-- end query -->
    <?php $c++ ?>
    <?php endforeach ?>
    </div>

Can someone help me? Thank you!

1 answer

0

Edit your custom post type template via template. https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/

When displaying categories instead of using a button

<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#<?php echo $catItem->slug?>" aria-expanded="<?php echo ($c == 2)?'true':'false'; ?>" aria-controls="<?php echo $catItem->slug?>">

Use a link

<a class="btn btn-link" title="Home" href="/depoimentos/'.$category->slug.'">'.$category->name.'</a>

In case you wanted to do something asynchronous you will have to do something like this post https://rudrastyh.com/wordpress/ajax-post-filters.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.