gpt4 book ai didi

php - wp_insert_post 带有表单

转载 作者:可可西里 更新时间:2023-11-01 00:07:14 27 4
gpt4 key购买 nike

<?php

$submitted = $_POST['submit'];
$post-title= $_POST['post_title'];
$post-content= $_POST['post_content'];


$new_post = array(
'post_title' => '$post-title',
'post_content' => '$post-content',
'post_status' => 'publish',
'post_author' => $user_ID,

);
if(isset($submitted)){
wp_insert_post($new_post);
}

?>

<form method="post" action=" ">
<input type="text" name="post_title" size="45" id="input-title"/>

<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>


<input type="hidden" name="cat" value="7,100"/>

<input class="subput round" type="submit" name="submit" value="Post"/>
</form>

我在没有表格的情况下对其进行了测试,它运行良好。但出于某种原因,我似乎无法让它与表格一起工作。有什么想法吗?

还有什么应该在表单的操作中进行?

感谢您的帮助。

最佳答案

这应该有效。

<?php 
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = $_POST['cat'];
$post_content = $_POST['post_content'];

$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_category' => array($post_category),
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish'
);

$post_id = wp_insert_post($new_post);

// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>

<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>

如果输入名称 new_post 且值为 0,则添加帖子。表单不需要任何操作,但您应该将 PHP 部分保留在页眉顶部。

关于php - wp_insert_post 带有表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4321914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com