2012-09-10 44 views
1

加載的wordpress後我試圖檢索使用其ID的WordPress的帖子的內容:錯誤,同時使用ID

$loadpost = url_to_postid($actual_link); 
$newpost = get_post($loadpost); 
echo '<article id="post-'.$newpost["ID"].'"><h1>'.$newpost["post_title"].'</h1>'.$newpost["post_content"]; 

$ loadpost返回一個有效的ID,但不知何故這個表達式不起作用。 IE返回:

Fatal error: Cannot use object of type stdClass as array in /hermes/waloraweb046/b428/moo.snippetspacecom/splittemplate/wp-content/themes/split/index.php on line 24 

這是什麼意思?

感謝您的幫助球員。

+0

http://stackoverflow.com/questions/6171699/cannot-use-object-of-type-stdclass-as-array-using-wordpress – anuragbh

回答

2

因爲get_post();默認情況下outputs as an OBJECT

你想回到什麼是

echo '<article id="post-'.$newpost->ID.'"><h1>'.$newpost->post_title.'</h1>'.$newpost->post_content; 
+0

相同的答案xlordt但更完整的感謝,它的偉大工程:)。 – cmplieger

1

改變所有[ '']至 - >示例

$newpost->ID; 
$newpost->post_title 

WP傳遞最參數作爲對象,而不是作爲陣列。

1

默認get_post返回一個對象,通過ARRAY_A作爲第二個參數返回一個關聯數組。

$loadpost = url_to_postid($actual_link); 
$newpost = get_post($loadpost, ARRAY_A); 
echo '<article id="post-'.$newpost["ID"].'"><h1>'.$newpost["post_title"].'</h1>'.$newpost["post_content"]; 
+0

沒有工作:( – cmplieger

+0

@SnippetSpace你叫'和'ARRAY_A' get_post'作爲第二個參數,或只是複製我的代碼?如果是後者有在我的代碼錯誤,如果現在被修正。 – Musa

+0

都嘗試,做似乎沒有工作兩種方式: - 。/ – cmplieger