wordpress - WordPress 自定义帖子类型前端
问题描述
我创建了一个自定义帖子类型“学生”,现在我希望该学生可以通过前端表单提交他们的详细信息。但是我使用年份和部门作为自定义分类法,我可以在前端将它们显示为下拉列表,但无法将字段值提交给有关分类法。我的代码如下:
<?php add_shortcode( 'singul_frontend_post', 'singul_frontend_post' );
function singul_frontend_post() {
singul_save_post_if_submitted();
?>
<div id="postbox">
<form id="new_post" name="new_post" method="post" >
<div class="form-group"><label for="title">Student Name</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" class="form-control" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="title">Year Of SSC</label><br />
<select name="ssc" class="form-control" searchable="A">
<?php
$tax_terms = get_terms('ssc', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="" selected disabled hidden>Choose Year</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select></div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="title">Group</label><br />
<select name="group" class="form-control" searchable="A">
<?php
$tax_terms = get_terms('group', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="" selected disabled hidden>Choose Group</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select></div></div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label for="title">Current Position</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="cposition" class="form-control" />
</div>
</div>
<div class="col-md-6"><div class="form-group"><label for="title">Current Institute</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="cinstitute" class="form-control" />
</div></div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label for="title">Contact Number</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="contact" class="form-control" placeholder="01XXXXXXXXX"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group"><label for="title">Email Address</label><br />
<input type="email" id="title" value="" tabindex="1" size="20" name="email" class="form-control" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group"><label for="title">Facebook ID</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="facebook" class="form-control" />
</div>
</div>
<div class="col-md-6">
<div class="form-group"><label for="title">Photo</label><br />
<input type="file" name="user-image-featured" id="user-image-featured" size="20" class="form-control-file" >
<?php wp_nonce_field( 'user-image-featured', 'user-image-featured_nonce' ); ?>
</div>
</div>
</div>
<?php wp_nonce_field( 'wps-frontend-post' ); ?>
<div class="form-group"><button type="submit" value="Publish" tabindex="6" id="submit" name="submit" class="btn btn-primary">Submit</button></div>
</form>
</div>
<?php
}
function singul_save_post_if_submitted() {
// Stop running function if form wasn't submitted
if ( !isset($_POST['title']) ) {
return;
}
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
echo 'Did not save because your form seemed to be invalid. Sorry';
return;
}
// Do some minor form validation to make sure there is content
if (strlen($_POST['title']) < 3) {
echo 'Please enter a title. Titles must be at least three characters long.';
return;
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => $_POST['title'],
'post_status' => 'draft', // Could be: publish
'post_type' => 'student' // Could be: `page` or your CPT
);
$cposition = $_POST['cposition'];
$institute = $_POST['cinstitute'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$fbid = $_POST['facebook'];
$ssc = $_POST['ssc'];
$group = $_POST['group'];
$post_id = wp_insert_post($post);
update_post_meta( $post_id, 'snp_student_cposition', $cposition );
update_post_meta( $post_id, 'snp_student_institute', $institute );
update_post_meta( $post_id, 'snp_student_contact', $contact );
update_post_meta( $post_id, 'snp_student_email', $email );
update_post_meta( $post_id, 'snp_student_fbid', $fbid );
wp_set_object_terms($post_id, $ssc, 'ssc');
wp_set_object_terms($post_id, $group, 'group');
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Let WordPress handle the upload.
// Remember, 'user-image-featured' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'user-image-featured', $post_id);
set_post_thumbnail( $post_id, $attachment_id );
echo 'Saved your post successfully! :)';
}
这里singul_field_name名称是元键。还考虑如何获取特色图片的附件ID。
**更新: 所有自定义元插入完成,只是不能插入帖子特色图片。
解决方案
您可以使用该wp_set_object_terms
功能保存自定义分类术语。
wp_set_object_terms( $post_id, $_POST['input_name_of_your_dropdown'], 'ssc', true );
wp_set_object_terms( $post_id, $_POST['input_name_of_your_dropdown'], 'group', true );
上面的代码应该放在后面wp_insert_post
以便$post_id
可以找到。
推荐阅读
- if-statement - Powerbi 使用标准表中使用的 IF 返回“NA”
- qt - 如何在 Qml 中为 3d 场景添加灯光?
- node.js - Nodejs 工作线程如何在后台工作?
- mongodb - MongoDB 聚合:如何展平对象?
- python - 将列从一个数据帧附加到另一个数据帧,循环中有多个匹配项
- c++ - 使用 Visual Studio 调试 C++ 默认生成的 == 运算符
- arrays - 从react js中的数组数据获取Img标签时如何设置图像路径?
- sockets - 如何使用通用 netlink 和 libnl 从内核向用户空间发送消息?
- r - 将坐标转换为国会选区
- ssh - Paramiko,打开一个 SOCKS 代理,但从机器 B 到 C