仅显示来自自定义分类和CPT的一个$Term(共4个术语)

时间:2020-03-24 作者:J. A.

我希望有人能对这个我找不到解决方案的问题有所了解!(我在www上搜索了几天)。

WHAT I HAVE DONE: 我创建了一个CPT:“service”和一个自定义分类法:“service\\u cat”,其中包含4个$terms: corporate events, fundraisers, private events and weddings.

我还创建了两个文件:

模板文件:services-page.php 显示四个术语,这似乎没有问题。

taxonomy-service_cat.php 每次显示一个术语,每次有人单击服务页面上的一个术语(其中显示四个术语)。

WHAT I WANT TO ACCOMPLISH:仅显示一个$term 以及每当单击其中一个术语时关联的帖子。

MY PROBLEM:每当我点击任何一个术语时,都会显示四个术语,包括相关的帖子。第一个术语正确显示在页面顶部,但在其下方显示其余术语和相关帖子。

这是我的代码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 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>

                                <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> 
注意:有些代码被注释掉了。

我真的希望有人能告诉我如何解决我的问题。万分感谢!!!

1 个回复
SO网友:J. A.

好的,经过几天的搜索,我终于解决了我的问题!!只需删除这两行代码:

$terms = wp_get_post_terms( $post->ID, \'service_cat\' );

                    foreach($terms as $term)  { 
改为使用此行:

$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );

这就解决了我的问题!!也要感谢这篇文章:https://www.smashingmagazine.com/2012/01/create-custom-taxonomies-wordpress/感谢那些试图帮助我的人!!

贾阿米拉