wp_insert_post无法在wp-admin中运行

时间:2014-05-31 21:33:34

标签: wordpress

我想知道一个Wordpress问题;试图创建一个wp-admin脚本,创建一个新的wp帖子......

以下是代码:

$my_post = array(
    'post_title'    => 'My post Gman99',
    'post_content'  => 'This is my post.',
    'post_status'   => 'draft',
    'post_author'   => 1,
    'post_category' => array(8,39)
);

$post_id = wp_insert_post( $my_post );
echo $post_id;

让我强调一下,如果我在前端"上运行它,这段代码就可以了。页;我只是将代码放在短代码中使用"短代码exec php"插件,它创建帖子没有问题,并吐出帖子ID;但是当我尝试在wp-admin中做同样的事情时它会失败吗?任何人都知道为什么会发生这种情况?

这是在wp-admin中运行的php文件的代码:

<?php
require_once('./admin.php');
if ( !current_user_can('edit_posts') )
wp_die(__('Cheatin&#8217; uh?'));
require_once('./admin-header.php');
?>
<div class="wrap">
<br/>
<?php
$my_post = array(
'post_title'    => 'My post Gman99',
'post_content'  => 'This is my post.',
'post_status'   => 'draft',
'post_author'   => 1,
'post_category' => array(8,39)
);
$post_id = wp_insert_post( $my_post );
echo $post_id;
echo "<br/>";
echo "<h2>You have completed entering your New Slide.</h2>";
echo "<br/>";
?>
</div>
<?php
include('./admin-footer.php');
?>

1 个答案:

答案 0 :(得分:-1)

我弄明白了,我自己的错误;但非常感谢Rahil愿意帮助......

基本上问题是我没有调用我认为的文件;基本上我插入帖子的代码在我认为的时候没有被运行;一个真正的新秀错误...

相关问题