0

好的,所以我的 php 很糟糕,所以在某些时候可能很难理解我的意思,但我几乎有一个 wordpress 循环,并且有一次我得到了帖子的类别并将其回显到课堂上。

有些帖子有多个类别,我想在课堂上回应它们。

到目前为止,这是我用来获取类别的

$categories = get_the_category(); 

这就是我回应他们的方式

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

如果我改为

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

它获得了第二类,但我希望它获得所有这些

这是完整的代码

<?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(); ?>
4

1 回答 1

2

如果要遍历所有类别数组,可以使用foreach如下语句:

<?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 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(); ?>  
于 2013-03-04T22:47:52.473 回答