开始。我对php很陌生,正在摸索。
我已经创建了一个表单(用于在前端捕获客户推荐信),并创建了一个名为“推荐信”的草稿自定义帖子类型,现在可以使用了(遗憾的是,没有PHP验证,只有脚本验证)
我现在得到的是:
我想做的是创建一个包含两个类别的下拉列表
实习公司,并将这些类别发布到我的自定义帖子类型中。
下面的PHP代码在我的函数中。php:
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST[\'title\'])) {
$title = $_POST[\'title\'];
} else {
echo \'Please enter a title\';
}
if (isset ($_POST[\'description\'])) {
$description = $_POST[\'description\'];
} else {
echo \'Please enter the content\';
}
$tags = $_POST[\'post_tags\'];
// Add the content of the form to $post as an array
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'tags_input\' => array($tags),
\'post_status\' => \'draft\', // do i need a comma after \'draft\'?
\'post_type\' => \'testimonial\' //do i need a comma after \'testimonial\'?
);
//save the new post
$pid = wp_insert_post($new_post);
header("Location: http://www.aipptraining.com.au/testimonial-submission-successful/");
exit;
}
表单代码:
<div class="form-group" id=="testimonialform">
<form action="" method="post" onsubmit="return validate();">
<label for="title">Full name</label>
<br/>
<input class="form-control" id="formtitle" name="title" type="text"/>
<br/>
<label for="description">Your experience</label>
<br/>
<textarea class="form-control" id="description" cols="40" name="description" rows="4"></textarea>
<input class="form-control" tabindex="5" name="post_tags" size="16" type="hidden" value="Testimonial" />
<br/>
<select name="categories" class="form-control" id="categories">
<option value="">Select</option>
<option value="intern">Intern</option>
<option value="host-company">Host Company</option>
</select>
<br/>
<input class="btn btn-default" id="submit" tabindex="6" name="submit" type="submit" value="Submit" />
<input name="action" type="hidden" value="new_post" /></form>
<!--?php wp_nonce_field( \'new-post\' ); ?-->
</div>
如果您的回答非常具体,我将不胜感激,因为我对PHP的理解有限。
此外,除了拉类别,如果你能帮助我验证的形式,这将是伟大的。我还试图为客户添加一个上传字段来上传他们的图像,但这对我来说太难了。
谢谢你MJ
SO网友:Malisa
这与您已经拥有的差不多,只需在后面添加以下内容$pid = wp_insert_post($new_post);
:
#categories
if( !empty ( $_POST [\'categories\'] ) ){
update_post_meta ( $pid, "_testimonials_category", stripslashes ( $_POST [\'categories\'] ) );
}
我可能会建议您将其名称从categories改为更为相对的名称,如Certifionals\\u categories
然后您可以使用:
$chosen_testimonial_category = get_post_meta($pid,\'_testimonials_category\',true);
至于验证,请查看
jQuery.validate