使用ID加载wordpress帖子时出错


error while loading wordpress post using ID

我正在尝试使用其ID:检索wordpress帖子的内容

$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

这是什么意思?

谢谢你们的帮助。

因为get_post();默认情况下输出为OBJECT

您要退回的是

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

将所有〔''〕更改为->示例

$newpost->ID;
$newpost->post_title

wp将大多数参数作为对象而不是数组传递。

默认情况下,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"];