簡體   English   中英

動態回顯 Wordpress 帖子的類別 Slug

[英]Dynamically Echo the Category Slug of Wordpress Posts

我需要為數據過濾器值回顯每個帖子的類別slug。 有人可以建議我做錯了什么嗎?

<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    
$categories = get_the_category(get_the_ID());
        foreach ($categories as $category){ 
            $slug = $category->slug;
?>
<div class="grid-item" data-filter="<?php echo $slug; ?>">
<?php
        }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM