[英]looping through Wordpress posts
我试图一次获取3个wordpress帖子,这是我正在使用的代码:
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'feature', true) != true) {; ?>
<div class="show_col">
<?php for ($i = 1; $i <= 3; $i++) { ?>
<div class="set">
<a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img class="image" src="http://localhost/portpress/wp-content/themes/myTemp/portfolio/<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>.jpg" width="300px" alt="AUREL #<?php the_ID(); ?>" />
</a>
<?php the_content("<P class='more'> Read More » </p>"); ?>
</div>
<?php }; ?>
</div>
<?php }; ?>
<?php endwhile; ?>
我知道这个循环三回响了一次-但是我真正想要的是这个最终结果
<div class="show_col">
<div class="show_col"> post1 </div>
<div class="show_col"> post2 </div>
<div class="show_col"> post3 </div>
</div>
<div class="show_col">
<div class="show_col"> post4 </div>
<div class="show_col"> post5 </div>
<div class="show_col"> post6 </div>
</div>
<!-- and so on -->
我这样做是因为每个帖子的高度各不相同-因此我添加了诸如.show_col {clear:both}之类的东西,以便接下来的三个帖子都整整齐齐
我希望你能帮忙
谢谢
这是您需要的代码(我更正了一些语法):
<?php $i = 0; ?>
<?php while ( have_posts() ): the_post();?>
<?php if(get_post_meta($post->ID, 'feature', true) != true): ?>
<div class="show_col">
<?php if(($i%3) == 0): ?>
</div>
<div class="show_col">
<?php endif;?>
<div class="set">
<a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img class="image" src="http://localhost/portpress/wp-content/themes/myTemp/portfolio/<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>.jpg" width="300px" alt="AUREL #<?php the_ID(); ?>" />
</a>
<?php the_content("<P class='more'> Read More » </p>"); ?>
</div>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
另外,您似乎在关闭函数( };
)之后添加了分号,不要那样做,不需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.