动态地回应Wordpress帖子的类别标签

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

我需要为每个帖子回显类别标签以获得数据过滤器值。有人可以告诉我我在做什么错吗?

<div id="container" class="isotope">
    <?php 
    $args = array(
        'post_type' => 'Photos',
    );
    $_posts = new WP_Query($args);
    ?>
    <?php 
    if ( $_posts->have_posts() ) : while ( $_posts->have_posts() ) : $_posts->the_post(); ?>

<!-- Here is the issue -->
    <div class="grid-item" data-filter="
        <?php 
        $categories = get_the_category(get_the_id());
        foreach ($categories as $category){ 
        echo $category->slug.' ';}?>"
    >

    <a onClick='showDialog()' data-target="#lightbox">
    <img src="<?php the_field('image'); ?>" alt="">
    </a>
    </div>
    <?php
    endwhile; endif;
    ?>
</div>
php wordpress post dynamic echo
1个回答
0
投票

请更改这样的代码

<?php    
$categories = get_the_category(get_the_ID());
        foreach ($categories as $category){ 
            $slug = $category->slug;
?>
<div class="grid-item" data-filter="<?php echo $slug; ?>">
<?php
        }
?>
© www.soinside.com 2019 - 2024. All rights reserved.