Categories
CMS Mastering Development Wordpress

Display ONLY ONE $term (Out of 4 terms) from a Custom Taxonomy and CPT

I hope someone can shed some light into this problem that i cannot find a solution for! (I have been searching the www for days).
WHAT I HAVE DONE:
I created a CPT: “service” and a Custom taxonomy: “service_cat”, which contains 4 $terms: corporate events, fundraisers, private events and weddings.
I also created two files:
a template file: services-page.php to display the four terms, which seems to work with no problem.
And taxonomy-service_cat.php to display one term at a time, every time someone clicks on one term on the services page (which displays the four terms).
WHAT I WANT TO ACCOMPLISH:
To display ONLY ONE $term and associated posts whenever one of the terms is clicked.
MY PROBLEM:
Whenever i click on any of the terms, the four terms get displayed including the associated posts. The first terms displays correctly at the top of the page, but underneath it it displays the remaining terms and associated posts.
This is my code for services-page.php:

<!-- Section -->
<section id="section-2" class="latestgallery-box">
            <div class="container text-center">
            <div class="row justify-content-center">
                    <div class="col-lg-8">
                        <h3 class="my-0 fs-1 fw-medium text-secondary "><?php //the_title(); ?>Add this title dinamically later</h3>

                        <h2 class="mb-5 fw-medium text-secondary text-uppercase"><?php the_field('services') ?></h2>
                        <p class="lead text-muted"> Add this dynamically too </p>
                    <?php //endif;?>
                    </div>
                </div>

                <!--===========Display the Services Terms ================-->

            <div class="row justify-content-center"><!--justify-content-center-->
                    <div class="col-lg-10">
                        <div class="row justify-content-center latestgallery-list mt-3">

                        <?php 
                       // $j = 0; 
                       // $k = 50;

                       //Used previously: terms = get_terms('process_category');
                        $terms = get_terms(array(
                            'taxonomy'=>'service_cat',
                            'hyde_empty' => false,
                            'orderby' => 'name'
                        ));
                           // ddd($terms);
                        foreach($terms as $term) : //{
                            $attachment_id = get_field('feature_image', $term);
                            $thumb = "svc-lg"; //thumbnail2 custom size for tax term
                            $image =  wp_get_attachment_image_src($attachment_id, $thumb);?> 


                         <!--Start col -->
                     <div class="col-12 mb-md-6 ">
                        <div class="latestgallery "> 
                             <a href="<?php 
                                 $term_link = get_term_link($term);
                                 if (is_wp_error($term_link)){
                                        continue;
                                 } echo esc_url($term_link);
                                 ?>" class="text-muted mark small">
                                  <img class=" center-block" src="<?php echo $image[0]; ?>" alt="<?php echo $term->name; ?>">
                                    <div class="image-caption-wrapper2">
                                        <div class="caption-text">
                                            <p><?php echo $term->name;?></p>
                                        </div>
                                    </div>   
                             </a> 
                        </div>
                     </div> 
                     <div class="col-12  "><!--col-12 col-md-6 col-lg-6 col-xl-5 -->
                                <div class="recentright-img mt-3 mb-5">

                                    <?php echo term_description($term);?>
                                     <a href="<?php
                                     $term_link = get_term_link($term);
                                     if (is_wp_error($term_link)){
                                       continue;
                                     } 
                                     echo esc_url($term_link); ?>" class="text-muted small"><em><?php _e('Read more');?> </em></a>
                                </div>
                        </div>

                           <?php 
                           endforeach; //}   ?>

                            </div>

                        </div><!--End col-lg-10-->
            </div> 

                    <a href="<?php echo site_url('/'); ?>" class="btn btn-secondary btn-lg mt-4 px-6"><?php _e('Learn more'); ?> <i class="zmdi zmdi-link ml-3"></i></a>

                </div> <!--End Container text-center-->
           <!-- </div>-->
        </section> 
        <!-- End of Section 2--> 

End of services-page.php code

===============================================================================

AND THIS IS THE CODE FOR taxonomy-service_cat.php

<section id="<?php echo get_the_id(); ?>" class="mt-5 pb-0"><!--section-2-->
    <div class="container">
            <div class="row"> 
                    <?php  
                    //$moreLink = '<a href="' . the_permalink() . '"> Read More...</a>';
                   // 
                   /* $terms = get_terms(array(
                        'taxonomy' => 'service_cat',
                        'hyde_empty' => false,
                        'orderby' => 'name' 
                        )); 
                        */
                    $terms = wp_get_post_terms( $post->ID, 'service_cat' );

                    foreach($terms as $term)  { 
                       // $i = 0;

                    $args = array(
                        //'name' => $the_slug,
                        'post_status' => 'publish',
                        'post_type' => 'service',
                        'posts_per_page' => 8,
                        'orderby' => 'title',
                        'order' => 'ASC',
                        'has_children' => true,
                        'tax_query' => array(
                            //'relation' => 'AND',
                            array(
                                'taxonomy' => 'service_cat',
                                'field' => 'slug',
                                'terms' => $term->slug, 
                                'operator' => 'IN'
                            ),
                        )
                        );

                     $fcservices = new WP_Query($args); 

                        $attachment_id = get_field('feature_image', $term);
                        $thumb = "svc-lg"; //thumbnail2 custom size for tax term
                        $image =  wp_get_attachment_image_url($attachment_id, $thumb);
                        $term_link = get_term_link($term);
                     ?>
                     <?php //print_r($attachment_id); ?>
                    <div class="col-lg-8 pb-6 pb-lg-0 order-lg-2">
                        <h2 id="fc-flowers" class="mb-5 text-uppercase"><?php echo $term->name;?>
                                </h2>     
                               <figure class="figure mb-4">
                                <a href="<?php 
                                    if (is_wp_error($term_link)){
                                       return;
                                   }
                                     echo esc_url($term_link); ?>" title="<?php //the_title_attribute(); ?>">
                                     <img src="<?php  echo $image;?>" class="pt-2 figure-img img-fluid" alt=""> </figcaption> <!--</a>-->
                                    <figcaption class="figure-caption text-right font-italic mb-5">- <?php echo $term->name; ?>-
                                </figure>

                        <div class="row justify-content-center">
                            <?php $i = 0;
                            if($fcservices->have_posts()): 
                            // $duplicates = array();  ?>
                            <div class="card-deck d-block d-lg-flex">
                            <?php while($fcservices->have_posts()) : $i++; if(($i % 2) == 0) : $fcservices->next_post(); else : $fcservices->the_post();  ?>


                                <div class="col-md-6" >
                                        <div class="card mb-3">
                                         <!--Display the post thumbnail --->
                                         <?php if(has_post_thumbnail()): ?>
                                         <a href="<?php  the_permalink();?>" alt="<?php the_title_attribute(); ?>"> 
                                        <?php
                                        the_post_thumbnail('service-thumb', array('class' => ' center-block')); ?>
                                        </a><?php endif;?>
                                            <div class="card-body">
                                                <h5 class="mb-4 fs-1 fw-medium text-secondary text-uppercase text-center"><!--<i class="zmdi zmdi-spinner mr-2"></i>--><?php the_title(); ?></h5>
                                                <p class="fs--1"><?php echo wp_trim_words(get_the_content(), 20);?></p>
                                                <p><a href="<?php  the_permalink();?>" class="small"><?php _e('read more..'); ?></a></p>
                                            </div>
                                        </div>
                                </div> <!--col-md-6-->

                                    <?php 

                                        endif;
                                        endwhile; 
                                        else: ?>
                                       <?php  endif; 
                                        $i = 0; 
                                        $fcservices->rewind_posts();  wp_reset_query();

                                      if ($fcservices->have_posts()) : 
                                        while($fcservices->have_posts()) : $i++; if(($i % 2) !== 0) : $fcservices->next_post(); else : $fcservices->the_post(); 

                                        //Check if the post is already displayed, if so, skip it
                                        //if(in_array(get_the_ID(), $duplicates)) ?> 
                                    <div class="col-md-6">
                                        <div class="card mb-3">

                                            <a href="<?php  the_permalink();?>"> 
                                            <?php
                                            the_post_thumbnail('service-thumb', array('class' => 'card-img-top', 'alt=')); ?>
                                            </a>
                                                <div class="card-body">
                                                <h5 class="mb-4 fs-1 fw-medium text-secondary text-uppercase text-center"><!--<i class="zmdi zmdi-male-female mr-2"></i>--><?php the_title(); ?></h5>
                                                <p class="fs--1"><?php echo wp_trim_words(get_the_content(), 20);?></p>
                                                <p class="small" ><a href="<?php  the_permalink();?>"><?php _e('read more..');?></a> </p>
                                                </div>
                                        </div>
                                    </div><!--col-md-6-->
                                <?php 

                            endif;
                            endwhile; else: ?>
                            <?php  wp_reset_query(); 
                            endif;  ?>

                            </div> <!--/card-deck d-block d-lg-flex-->

                            </div> <!--/end row justify-content-center-->
                       <?php //endif;?>

                            <h2 id="apply-now" class="mb-5 text-uppercase mt-5"><?php _e('Contact Us');?></h2>
                            <div class="row">
                            <?php echo do_shortcode('[contact-form-7 404 "Not Found"]'); ?>
                            </div>

                </div>  <!--End Col-8 -->

                        <!--=======Start col-4/ Left sidebar========-->

                <div class="col-lg-4 pl-lg-5 order-lg-1">
                        <h5 class="mb-4 text-uppercase text-400">Main Services</h5>
                            <div data-toggle="sticky" data-sticky-offset-top="150" style="" class="">
                                <ul class="mb-5 mb-lg-6 pl-4 text-600"> 

                                   <?php 
                                    $terms = get_terms(array(
                                        'taxonomy' => 'service_cat',
                                        'hyde_empty' => false,
                                        'orderby' => 'name' //previously: 'name' 
                                        ));

                                    foreach($terms as $term) : //{ ?>
                                    <li class="mb-1 " ><a href="<?php
                                     $term_link = get_term_link($term);
                                      if (is_wp_error($term_link)){
                                        continue;
                                    } echo esc_url($term_link); ?>"  class=" text-secondary text-600"><?php echo $term->name; ?></a></li>
                                    <?php  endforeach; // } 
                                    wp_reset_postdata();  ?> 
                                </ul>

                                <h5 class="mb-4 text-uppercase text-600">Share</h5>
                                <div class="mb-5  mb-lg-6 bg-secondary">
                                <ul class="nav flex-nowrap ml-lg-6"><?php 
                               // if ( is_active_sidebar('gdsocial-sidebar') ) : ?>
                                <?php //dynamic_sidebar('gdsocial-sidebar'); ?>
                                </ul><?php //endif; ?>

                                </div>

                                <h5 class="mb-4 text-uppercase text-600"><?php 
                                 //   $post_tags = get_the_tags();
                                 //if (!empty($post_tags)){ echo 'Tags';} elseif($post_tags);  ?></h5>
                                <div class="mb-5 mb-lg-6">
                                <?php 
                                   //  $separator = '  '; 
                                   //  $output = '';
                                    // while(have_posts()) : the_post();
                                      //  if (!empty($post_tags)){
                                      //  foreach($post_tags as $tag) { 

                                      //     $output .= '<a href="#" class="badge badge-light py-2 px-4 mr-2 mb-2 text-700 text-uppercase fs--2">' . $tag->name . '</a>' . $separator;
                                      //  }
                                     //   echo trim($output, $separator); ?>
                                        <?php //} ?>
                                </div>

                            </div>
                </div> 

            </div> <!--end .row-->

        </div><!--end .container--><?php   } ?> 
     </section> 

Note: There are some code that is commented out
I really hope someone can show me how to solve my issue. A thousand thanks in advance!!!

JA Armira

Leave a Reply

Your email address will not be published. Required fields are marked *