Php 在wordpress中仅显示自定义分类图像
编写PHP的新手。 我已经创建了CPT(画廊)与八个职位。 我还创建了具有八个类别的自定义分类法(category)。 使用高级自定义字段,我上传了一张分类图片。 有两个职位具有相同的类别 “我只想在一页中显示类别图像和名称。” 单击该类别名称或图像后,它应该移动到另一个页面,显示与该类别相关的帖子。 Bt我在显示类别图像和名称时遇到问题。 对于我的代码,它显示了两次类别图像和名称,因为两篇文章具有相同的类别 [前端]Php 在wordpress中仅显示自定义分类图像,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,编写PHP的新手。 我已经创建了CPT(画廊)与八个职位。 我还创建了具有八个类别的自定义分类法(category)。 使用高级自定义字段,我上传了一张分类图片。 有两个职位具有相同的类别 “我只想在一页中显示类别图像和名称。” 单击该类别名称或图像后,它应该移动到另一个页面,显示与该类别相关的帖子。 Bt我在显示类别图像和名称时遇到问题。 对于我的代码,它显示了两次类别图像和名称,因为两篇文章具有相同的类别 [前端] <?php /* Template Name: hometemp*/
<?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
$terms = get_terms( array(
'taxonomy' => 'category',
'orderby' => 'count',
'hide_empty' => false,
'fields' => 'all'
) );
?>
<?php
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
?>
<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('test_one', $term);?>
<img src="<?php echo $image;?>" alt="" srcset="" height=400 width=300>
</div>
</div>
</div>
</a>
<?php }?>