我正在尝试获取带有相对帖子的自定义帖子类型"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; ?>
试试这个:
<?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; ?>