我试图解决这个问题几个小时,但我不能。我只是想显示第二篇文章并跳过第一篇文章,就像Wordpress偏移函数一样。我正在为Magento使用AW博客扩展,我想跳过最近的博客帖子中的第一篇文章。下面是我修改过的代码,显示了主页块中最近发布的帖子。我只是想创建另一个块来显示最近的第二篇文章。 :(
<?php $posts = $this->getPosts(); ?>
<div id="messages_product_view">
<?php Mage::app()->getLayout()->getMessagesBlock()-> setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
<?php echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml(); ?>
</div>
<?php
foreach ($posts as $post):
if ($i++ >= 1) break;
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php endforeach; ?>
答案 0 :(得分:1)
我建议你检查计数,打印后停止循环,否则它会将整个数组循环到最后并打印第二次迭代。
$i=0;
foreach ($posts as $post){
if ($i==1) {
//print your div here
$i++; //added here after edit.
continue;
}else if ( $i>1 ){
break;
}
$i++;
}
这样它只会迭代两次。
答案 1 :(得分:0)
尝试更改:
foreach ($posts as $post):
if ($i++ >= 1) break;
为:
//set $i outside of loop
$i=0;
foreach ($posts as $post):
if ($i==0) {
$i++;
//skip the first record
continue;
}
答案 2 :(得分:0)
试试这个 -
<?php $i=1;
foreach ($posts as $post):
if ($i > 1) {
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php }
$i++;
endforeach; ?>