【问题标题】:How to exclude custom post type Tag from the loop?如何从循环中排除自定义帖子类型标签?
【发布时间】:2022-01-20 15:23:37
【问题描述】:

我正在尝试使用相关帖子获取自定义帖子类型"Services" 的所有类别,但它也显示标签,我想从类别列表中排除服务分类标签。对不起,我坚持要解决这个问题。请问有人可以帮我解决吗?

<?php
$post_type = 'services';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type,  ) );
 
foreach( $taxonomies as $taxonomy ) :
 
    $terms = get_terms( $taxonomy );
 
    foreach( $terms as $term ) : ?>

        <?php
        $args = array(
                'post_type' => $post_type,
                'posts_per_page' => -1,  //show all posts
                'tax_query' => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'slug',
                        'terms' => $term->slug,
                    )
                    
                )
 
            );
        $posts = new WP_Query($args);
 
        if( $posts->have_posts() ): ?> 
        <section class="mb-5 pb-5">
    
    <div class="container">
        <div class="row text-center mb-5">
        <h2 class="h4"><?php echo $term->name; ?></h2>
        </div>
        <div class="row justify-content-center">
        <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
        

        <div class="col-6 col-md-3 mb-3 mb-lg-0">
          <div class="card card-style-1 shadow-lg border-0 text-center">
          <a href="<?php echo get_the_permalink();?>" class="position-absolute top-0 bottom-0 w-100 h-100"></a>
            <div class="mb-3">
              <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="" class="img-fluid">
            </div>
            <div class="card-title">
              <h3 class="h6 fw-normal"><?php echo the_title(); ?></h3>
            </div>
          </div>
        </div>      
                    
        <?php endwhile; 
       ?>
       </div>
       </div>
</section>

       <?php
      endif; ?>
 
    <?php endforeach;
 
endforeach; ?>

【问题讨论】:

    标签: javascript php html wordpress wordpress-theming


    【解决方案1】:

    试试这个:

    <?php
    $post_type = 'services';
    $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type,  ) );
    $exclude = array( 'post_tag' );
     
    foreach( $taxonomies as $taxonomy ) :
    
        if( in_array( $taxonomy->name, $exclude ) ) {
                continue;
        }
    
     
        $terms = get_terms( $taxonomy );
     
        foreach( $terms as $term ) : ?>
    
            <?php
            $args = array(
                    'post_type' => $post_type,
                    'posts_per_page' => -1,  //show all posts
                    'tax_query' => array(
                        array(
                            'taxonomy' => $taxonomy,
                            'field' => 'slug',
                            'terms' => $term->slug,
                        )
                        
                    )
     
                );
            $posts = new WP_Query($args);
     
            if( $posts->have_posts() ): ?> 
            <section class="mb-5 pb-5">
        
        <div class="container">
            <div class="row text-center mb-5">
            <h2 class="h4"><?php echo $term->name; ?></h2>
            </div>
            <div class="row justify-content-center">
            <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            
    
            <div class="col-6 col-md-3 mb-3 mb-lg-0">
              <div class="card card-style-1 shadow-lg border-0 text-center">
              <a href="<?php echo get_the_permalink();?>" class="position-absolute top-0 bottom-0 w-100 h-100"></a>
                <div class="mb-3">
                  <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="" class="img-fluid">
                </div>
                <div class="card-title">
                  <h3 class="h6 fw-normal"><?php echo the_title(); ?></h3>
                </div>
              </div>
            </div>      
                        
            <?php endwhile; 
           ?>
           </div>
           </div>
    </section>
    
           <?php
          endif; ?>
     
        <?php endforeach;
     
    endforeach; ?>
    

    【讨论】:

    • 不工作的兄弟
    • @Devbuddy 您是否将排除的分类名称粘贴到数组 $exclude = array( ); ?
    • 不,兄弟,我只是粘贴您的代码。实际上我想删除所有标签而不是数组中名称的特定标签。这可能吗?
    • 我能得到任何解决方案吗?
    • @Devbuddy 标签是一种分类法。您需要知道名称才能排除它
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 2019-09-30
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    相关资源
    最近更新 更多