我想獲得最新的WordPress的帖子在我的網站的頂部和這篇文章下面,我試圖顯示所選頁面的內容。例如如果用戶在「首頁」上,則應顯示最新帖子,並在其下方顯示「主頁」的內容。要獲取最新帖子,我正在使用wp_get_recent_posts()
並顯示我正在使用的網頁內容the_content()
。使用wp_get_recent_posts()和the_content()在同一時間使用wordpress
這是我最新的郵編:
<div id="news">
<?php
$posts = wp_get_recent_posts(array('numberposts' => 1, 'post_type' => 'post'));
foreach($posts as $post){
echo '<p>' . $post["post_content"] . '</p>';
}
?>
</div>
,並顯示出我使用這個代碼的頁面內容:
<h2><?php the_title(); ?> </h2>
<div id="postcontent">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>
</div>
我的問題是:它不顯示頁面內容。如果我評論「新聞」部分,它會顯示頁面內容。看起來這些代碼部分不能同時工作。有沒有其他選擇,或者我的代碼中有什麼問題?
非常感謝:)它按預期工作。我注意到我的代碼的另一種選擇是重命名'$ posts'和'$ post'。 – Tyler
對不起,被'get_posts'帶着帶走 –
如果你堅持你的方法是,但是如果你使用'setup_postdata',你必須使用'$ post' ;-) –