0
我目前正在嘗試開發一個自定義的WordPress主題,並且在我的主頁上我需要添加第二個內容塊。我正在使用一個插件來做到這一點,它只是要求我在需要內容塊時添加以下內容。繼續WordPress的PHP循環
<?php the_block('Latest Products')?>
但是,當我添加這個似乎沒有效果,我相信是由於我的PHP的格式。我相當新的PHP,所以任何幫助,不勝感激。
我的代碼如下 - 我剪掉了HTML的最好的部分。我認爲這與'endforeach'標籤有關?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php the_block('Latest Products')?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
編輯
好了,顯然它需要環路以外的地方放,但它仍然是行不通的。有任何想法嗎?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php endwhile; endif; ?>
<?php the_block('Latest Products')?>
<?php get_footer(); ?>
感謝瑞恩 - 沒有足夠的數據來確定它的相容性,但 - 一個人說,它的工作原理但就是這些。出於某種原因,我只是在頁面編輯器中看到第二個可編輯區域,而它不在循環中。如果把它放在循環內部,特別是在調用最新4個帖子的部分上面,即使在循環內部也可以正常工作,但顯然我需要它在下面。這導致我認爲該代碼有些不正確,但我沒有足夠的經驗足以說明問題。 – 2013-03-11 00:05:04
我對我的答案進行了編輯,以解決問題。你的代碼是正確的,它只是需要特定順序的插件。 – 2013-03-11 18:57:53