好吧所以我的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(); ?>
答案 0 :(得分: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(); ?>