dongtu4028 2013-03-04 22:43
浏览 35
已采纳

从wp数组中回显多个类别

Ok so my php is pretty bad, so it may be hard to understand what I mean at some points but pretty much I have a wordpress loop and at one point I get the category of the post and echo it into the class.

Some posts have multiple categories and I want to echo them both into the class.

So far this is what I use to get the categories

$categories = get_the_category(); 

and this is how I echo them

<?php echo $categories[0]->category_nicename; ?>

If I change to

<?php echo $categories[1]->category_nicename; ?> 

it gets the second category but I want it to get all of them

This is the full code

<?php query_posts("post_type=portfolio"); ?>
<?php $i=0; /** start the project loop here */?>
<?php if(have_posts()):?>
<?php while(have_posts()) : the_post();?> 
<?php $i++; ?>

<?php
$image1ID = get_field('thumbnail');
$image1 = wp_get_attachment_image_src( $image1ID, '500by250-thumb' );
$attachment = get_post( $image1ID );
$image1_title = $attachment->post_title;
$categories = get_the_category(); 

?>          

    <pre>
        <?php print_r($categories) ?>
    </pre>

    <li data-type="<?php echo $categories[0]->category_nicename; ?>" data-id="id-<?php echo $i ?>" class="portfolio-thumb <?php the_field('thumb_size');?>">

        <a data-scroll="#portfolio-second" href="<?php the_permalink(); ?>" title="<?php echo $image_title; ?>">
            <div class="hover-content">
                <span class="thumb-caption"><?php the_field('thumb_caption');?></span>
                <span class="thumb-title"><?php the_title();?></span>
            </div>
            <img src="<?php echo $image1[0] ?>" alt="<?php echo $image_title; ?>">
        </a>

    </li>   

<?php endwhile; ?>
<?php else: ?>  
<?php endif; ?>
<?php wp_reset_query(); ?>
  • 写回答

1条回答 默认 最新

  • dsndm82062 2013-03-04 22:47
    关注

    If you want to loop through all categories array, you can use foreach statement like below:

    <?php foreach($categories as $category){?>
        <li data-type="<?php echo $category->category_nicename; ?>" data-id="id-<?php echo $i ?>" class="portfolio-thumb <?php the_field('thumb_size');?>">
    
                            <a data-scroll="#portfolio-second" href="<?php the_permalink(); ?>" title="<?php echo $image_title; ?>">
                                <div class="hover-content">
                                    <span class="thumb-caption"><?php the_field('thumb_caption');?></span>
                                    <span class="thumb-title"><?php the_title();?></span>
                                </div>
                                <img src="<?php echo $image1[0] ?>" alt="<?php echo $image_title; ?>">
                            </a>
    
                        </li> 
    <?php } ?>
    

    The full code would be:

            <?php query_posts("post_type=portfolio"); ?>
            <?php $i=0; /** start the project loop here */?>
            <?php if(have_posts()):?>
            <?php while(have_posts()) : the_post();?> 
            <?php $i++; ?>
    
            <?php
            $image1ID = get_field('thumbnail');
            $image1 = wp_get_attachment_image_src( $image1ID, '500by250-thumb' );
            $attachment = get_post( $image1ID );
            $image1_title = $attachment->post_title;
            $categories = get_the_category(); 
    
            ?>          
    
                <pre>
                    <?php print_r($categories) ?>
                </pre>
    
                <?php foreach($categories as $category){?>
        <li data-type="<?php echo $category->category_nicename; ?>" data-id="id-<?php echo $i ?>" class="portfolio-thumb <?php the_field('thumb_size');?>">
    
                            <a data-scroll="#portfolio-second" href="<?php the_permalink(); ?>" title="<?php echo $image_title; ?>">
                                <div class="hover-content">
                                    <span class="thumb-caption"><?php the_field('thumb_caption');?></span>
                                    <span class="thumb-title"><?php the_title();?></span>
                                </div>
                                <img src="<?php echo $image1[0] ?>" alt="<?php echo $image_title; ?>">
                            </a>
    
                        </li> 
    <?php } ?>
            <?php endwhile; ?>
            <?php else: ?>  
            <?php endif; ?>
            <?php wp_reset_query(); ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀