WordPress 自定义帖子类型前端
Posted
技术标签:
【中文标题】WordPress 自定义帖子类型前端【英文标题】:WordPress Custom Post Type FrontEnd 【发布时间】:2021-12-25 22:16:17 【问题描述】:我创建了一个自定义帖子类型“学生”,现在我希望该学生可以通过前端表单提交他们的详细信息。但是我使用年份和部门作为自定义分类法,我可以在前端将它们显示为下拉列表,但不能将字段值提交给有关分类法。我的代码如下:
<?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 名称是元键。还在思考如何获取特色图片的attachment_id。
**更新: 所有自定义元插入完成,只是不能插入帖子特色图片。
【问题讨论】:
【参考方案1】:您可以使用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
。
【讨论】:
感谢完美。 @MahbulAlam 如果答案有帮助,请点赞并将答案标记为正确。以便未来的访问者可以将这个问题视为已解决。 你能检查我的更新部分吗?我不能只更新 post_featured 图像。 @MahbulAlam 您在表单标签中缺少enctype="multipart/form-data"
编码属性。当表单处理二进制内容(例如文件)时,这是必需的。以上是关于WordPress 自定义帖子类型前端的主要内容,如果未能解决你的问题,请参考以下文章