我使用WP插件「自定義帖子類型UI」和我自己的模板來添加一些內容到我的wp頁面。WP自定義帖子類型UI
<div id="AboutMain">
<?php
/* the original page contents */
while (have_posts()) {
the_post();
get_template_part('content', 'page');
/* New set of data fetched from my custom post types */
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'styrelse'
));
if($posts)
{
foreach($posts as $post)
{
$namn=get_the_title($post->ID);
$desc=get_the_content($post->ID);
echo "
<div class='styrelsen art-postcontent'>
<h5 class='art-postcontent'>$namn</h5>
<p>$desc</p>
</div>
";
}
}
現在我得到一個非常奇怪的行爲 - 因爲我有一個自定義查詢設置$崗位,在for-each循環不應該包含基本的網頁內容了,但我的自定義後類型的內容吧?
但是get_the_content($ post-> ID);取得頁面的原始內容,並在get_the_title($ post-> ID)中反覆重複;爲我提供正確的標題。
** * ** * ** 解決 ** * ** * ** * ****'
OK ,這是我是如何做到的。
首先我意識到使用該全局變量名稱可能不是最好的idéa,所以我將其更改爲$ board_posts。
然後,我傾倒了這個變量,以找出如何訪問標題和「手動」的身體,事實證明,這比我第一次想到的容易。
foreach($styrelse_posts as $post)
{
$namn = $post->post_title;
$desc = $post->post_content;
試錯節拍的大多數問題=)
http://codex.wordpress.org/Template_Tags/get_posts – hakre 2012-02-28 07:51:16