【发布时间】:2019-12-10 11:13:49
【问题描述】:
编写 PHP 的新手。 我创建了包含 8 个帖子的 CPT(图库)。 我还创建了具有八个类别的自定义分类法(类别)。 使用高级自定义字段,我已经上传了一张分类图片。 有两个帖子的类别相同。
“我只想在一页中显示类别图像和名称。”
单击该类别名称或图像后,它应该会移动到显示与该类别相关的帖子的另一个页面。 Bt 我在显示类别图像和名称时遇到问题。 对于我的代码,它显示两倍的类别图像和名称,因为两个帖子具有相同的类别。
[前端]
<?php /* Template Name: hometemp*/ ?>
<?php get_header(); ?>
<?php
$terms = get_terms( array(
'taxonomy' => 'categorie',
'orderby' => 'count',
'hide_empty' => false,
'fields' => 'all'
) );
?>
<?php
foreach( $terms as $term ) {
$args = array(
'post_type' => 'gallery',
'categorie' => $term->slug
);
$term_link = get_term_link( $term );
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
/* Start the Loop */
while ( $query->have_posts() ) : $query->the_post(); ?>
<a class="property-thumb-link"
href="<?php echo $term_link; ?>">
<div class="property-thumb column medium-6 small-12">
<div class="property-thumb-title">
<h2>
<div class="property-thumb-title">
<h2>
<?php echo $term->name; ?>
</h2>
</div>
</h2>
</div>
<div>
<div >
<?php $image = get_field('category_image', $term);?>
<img src="<?php echo $image['url'];?>" alt="" srcset="" height=400 width=300>
</div>
</div>
</div>
</a>
<?php endwhile;
wp_reset_postdata();
endif; }?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
【问题讨论】:
标签: php wordpress custom-post-type