仅在wordpress中显示自定义类别图像

问题描述 投票:0回答:1

是编写PHP的新手。我创建了8个帖子的CPT(画廊)。我还创建了具有八个类别的自定义分类法(类别)。使用“高级自定义字段”,我已经上传了一张图片。有两个帖子具有相同的类别。

“我只希望在一页中显示类别图像和名称。”

单击该类别名称或图像后,它应移至另一个页面,显示该类别的相关帖子。我在显示类别图像和名称时遇到问题。对于我的代码,它显示的是类别图像和名称的两倍,因为两个帖子具有相同的类别。

[[前端]

<?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
1个回答
0
投票

请尝试以下代码:

<?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 }?>
© www.soinside.com 2019 - 2024. All rights reserved.