php - 在 wordpress 中仅显示自定义类别图像
问题描述
编写 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
$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 }?>
推荐阅读
- php - 动态表格和数据库
- python - 如何将 pytest.mark.parametrize 与固定装置一起使用
- python - 如何在python中找到对称平均绝对误差?
- reactjs - 错误:处理“@@INIT”操作时,Reducer“auth”返回未定义。要忽略一个动作,您必须显式返回之前的状态
- javascript - async.map 不调用回调
- ruby-on-rails - 解析和提取 JSON 数据
- ios - 应用程序卡在特定屏幕中而没有任何错误
- ios - UICollectionView 的 scrollToItemAtIndexPath 崩溃
- python - 关于数据操作的查询
- python - AttributeError 'NoneType' 对象没有属性 'upload_from_filename'