• 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 Process 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; //place the counter inside the loop does not work!
                           // $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 Website Design Services Area -->
                             <!--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> <!--row justify-content-center-->
                         
                        <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>--><?php //echo get_the_ID(); ?>
            </section> 
            <!-- End of Section 2-->
    

    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>';
                       // Another way to get the terms//
                       /* $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, //Previously: array('creative'), wasn't working
                                    '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 id="20" html_class="col-lg-10 col-xl-9" title="GDWebPros2 Contact Form"]'); ?>
                                </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>
                                     <?php 
                                    /* echo '<pre>';
                                     var_dump($terms); 
                                     echo '</pre>'; */
                                     ?>
                                    <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> 

    I really hope someone can show me how to solve my issue. A thousand thanks in advance!!!

    JA Armira

    • This topic was modified 4 years, 11 months ago by gdweb01.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • I didn’t read all your code, but one thing you have to keep in mind is that all theme template files are for showing posts (not taxonomies, not terms, not users). So even something that is named like a taxonomy term, it is simply an archive for the posts with that term.
    That is what WordPress does by default.
    Your code looks like it is doing a lot of other things, getting terms and posts and whatever else (I didn’t read the code). Don’t try to rewrite what WordPress is already doing. Just use the standard loop to display the posts from the query that WordPress already did, and if your post type has something extra, then you can do that extra thing in the loop. But there are core functions for showing the correct post and its taxonomies without much effort on your part.

    @gdweb01
    There was also a comment to more or less the same question in https://www.ads-software.com/support/topic/display-one-term-out-of-4-from-a-taxonomy-taxonomy-php-cpt/ which I closed. I left this one open, since this version of the question contains a bit more information.

    Thread Starter gdweb01

    (@gdweb01)

    Thank you very much to both Joy and Tor.
    I appreciate your comments and will keep that in mind.
    Any suggestions to solve my problem?
    How to go about to fix what i have done wrong?

    Thanks in advance again!!

    Moderator bcworkz

    (@bcworkz)

    What I suggested in the now closed duplicate topic, and what Joy suggests here, amounts to the same solution. Start with a copy of your theme’s default taxonomy.php or archive.php template. It’ll do what you describe without any change. If you need to add more content for your particular post type, do so in the standard loop. You can also add term related content outside the loop if need be, but don’t do any special queries, let WP manage the term archive query.

    Thread Starter gdweb01

    (@gdweb01)

    Understand your suggestion and appreciate your comment.
    This is a custom theme that started to design and develop from scratch, so i don’t have a taxonomy.php file or archive.php. I am very new to php and still is hard for me to understand a lot of WordPress code.
    Would be viable to copy the archive.pho from twentynineteen theme for example?
    Many thanks!!

    Read the Theme Developer Handbook, and especially the Template Hierarchy.
    No, don’t copy a different theme’s archive.php because you have a different header.php and footer.php and totally different markup that your styles match.
    A theme really only needs index.php. The other template files are all the same as that with a tweak here and there for single versus archive or page versus post, (or search).

    Just use the standard loop and see how it behaves, then change from there.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display ONE $term (Out of 4 ) from a taxonomy-{taxonomy}.php & CPT’ is closed to new replies.