如果我的问题看起来很愚蠢,我是WordPress的新手。所以道歉。
我开发了以下自定义表单,以便从前端发布特定的帖子类型story
。我遇到的问题是自动生成的永久链接。当我尝试访问该页面时,它会将我带回主页。我为我的帖子类型创建了一个自定义页面single-story.php
,我认为它会自动访问但不是。
<?php
/* Post type: story */
add_shortcode('story', 'trip_story_form_builder_shortcode');
function trip_story_form_builder_shortcode(){
if($_POST['story'] == 'submit' && !empty($_POST['action'])){
//echo 'Ok';
}
if(isset($_POST['txtTitle'])){
$title = $_POST['txtTitle'];
} else {
//echo 'Please add a description';
} ?>
<!-- form starts -->
<form method="post" name="story_form" action="" id="story_form">
<div class="text-small">
<div class="row margin-top-10">
<div class="col-md-12">
Story Title <i class="text-red">*</i>
<input type="text" class="form-control" id="txtTitle" name="txtTitle" maxlength="75" required />
</div>
</div>
<div class="row margin-top-10">
<div class="col-md-12">
Story <i class="text-red">*</i>
<div class="margin-top-5"></div>
<?php
$content = '';
$editor_id = 'txtStory';
$settings = array(
'textarea_name'=> 'txtStory',
'quicktags' => false,
'media_buttons' => true,
'teeny' => false,
'tinymce' => array(
'toolbar1'=> 'bold,italic,underline,bullist,link,unlink,forecolor,undo,redo'
)
);
wp_editor($content, $editor_id, $settings);
?>
</div>
</div>
<div class="row margin-top-10">
<div class="col-md-8">
Time of visit:
<div class="row margin-top-5">
<div class="col-md-6">
Year<i class="text-red">*</i>
<input type="text" class="form-control" id="txtYear" name="txtYear" maxlength="4" required />
</div>
<div class="col-md-6">
Month<i class="text-red">*</i>
<select id="cboMonth" name="cboMonth" class="form-control" required>
<option value="">-</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row margin-top-25">
<div class="col-md-12">
No. of heads <i class="text-red">*</i>
<input id="txtHeads" name="txtHeads" type="number" maxlength="3" class="form-control" required />
</div>
</div>
</div>
</div>
<div class="row margin-top-10">
<div class="col-md-12">
Places visited <i class="text-red">*</i>
<input type="text" class="form-control" id="txtPlaces" name="txtPlaces" maxlength="300" required placeholder="Enter name of the places you visited. Separate places by comma(,)" />
</div>
</div>
<div class="row margin-top-10">
<div class="col-md-6">
Category <i class="text-red">*</i>
<?php
$args = array(
'show_option_all' => '',
'show_option_none' => '',
'option_none_value' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'include' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cboCategory',
'id' => 'cboCategory',
'class' => 'form-control',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => true,
'value_field' => 'term_id',
);
wp_dropdown_categories( $args );
?>
</div>
<div class="col-md-6">
Total Trip cost <i class="text-red">*</i>
<input type="number" id="txtCost" name="txtCost" required maxlength="6" class="form-control" />
</div>
</div>
<div class="row margin-top-10">
<div class="col-md-12">
Additional information, if any (max. 600 characters)
<textarea id="txtInfo" name="txtInfo" rows="5" cols="80" maxlength="600" class="form-control" placeholder="For example, name of hotel you stayed, name and phone numebr of your cab driver etc."></textarea>
</div>
</div>
<div class="row margin-top-20">
<div class="col-md-12">
<input type="hidden" name="story" value="submit" />
<input type="hidden" name="action" value="new_story" />
<?php wp_nonce_field( 'new_story' ); ?>
<input type="submit" value="Submit Trip Report" class="btn btn-primary text-medium-small">
</div>
</div>
</div>
</form>
<?php
}
function add_trip_story() {
if($_POST['story']=="submit" && !empty( $_POST['action'] )) {
$title = $_POST['txtTitle'];
$description = $_POST['txtStory'];
//meta data builder
$visiting_year = $_POST['txtYear'];
$visiting_month = $_POST['cboMonth'];
$no_heads = $_POST['txtHeads'];
$places = $_POST['txtPlaces'];
$trip_cost = $_POST['txtCost'];
$addl_info = $_POST['txtInfo'];
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_type' =>'story',
'post_status' => 'publish'
);
//insert the the post into database by passing $new_post to wp_insert_post
$pid = wp_insert_post($new_post);
//We now use $pid (post id) to help add our post meta data
add_post_meta($pid, '_visting_year', $visiting_year, true);
add_post_meta($pid, '_visiting_month', $visiting_month, true);
add_post_meta($pid, '_visiting_heads', $no_heads, true);
add_post_meta($pid, '_visiting_places', $places, true);
add_post_meta($pid, '_visiting_cost', $trip_cost, true);
add_post_meta($pid, '_visting_addl_info', $addl_info, true);
}
}
add_action('init','add_trip_story');
上述表单在数据库中完美呈现并保存数据。
我想将一个story
加载为local.tourplanner.com/story/title-of-the-story。 WordPress生成的固定链接是http://local.tourplanner.com/winter-vacation-at-auli-a-brief-report/
,它总是带我到主页。
但是如何告诉WordPress为我的story
帖子类型使用特定模式。有没有办法在上面的代码中定义它?
我可以问几件事吗?
如何使用wp-admin
功能在register_post_type
内注册此帖子类型时,如何在custom post type
内看到这些帖子?
我在表单中有一个类别下拉列表。如何告诉WordPress将其用作子链接并加载与此类别相关的帖子?
我很抱歉在一个帖子中提出了多个问题。我只是想把它们放在一个地方,因为它们看起来很相关。
更新
我已经放弃了以远远的方式存储数据的想法,并通过使用register_post_type
函数注册来创建function render_user_story_form() {
$form='<form id="user_story_form" name="user_story_form" action="" method="post">';
$form .= '<div><input type="text" class="form-control" id="_title" name="_title" maxlength="75" required placeholder="Story title" /></div>';
$form .= '<div><textarea id="_description" name="_description" rows="5" cols="80" class="form-control" maxlength="500" required placeholder="Your story here"></textarea></div>';
$form .= '<div><input type="text" class="form-control" id="_places_visited" name="_places_visited" maxlength="100" required placeholder="Places visited during the trip" /></div>';
$form .= '<div><input type="number" class="form-control" id="_trip_cost" name="_trip_cost" maxlength="6" required placeholder="Trip cost" /></div>';
$form .= '<input type="submit" value="Submit" class="btn btn-primary" />';
$form .= '</form>';
echo $form;
}
add_shortcode('new-user-story', 'render_user_story_form');
。
呈现表单的功能:
wp_posts
以下是我在wp_postmeta
和function save_user_story() {
$title = $_POST['_title'];
$description = $_POST['_description'];
$places = $_POST['_places_visited'];
$cost = $_POST['_trip_cost'];
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_type' => 'user_story',
'post_status' => 'publish'
);
$new_post_id = wp_insert_post($post, 10, 1);
add_post_meta($new_post_id, '_places_visited', $places);
add_post_meta($new_post_id, '_trip_cost', $cost);
}
add_action('save_post_user_story', 'save_user_story', 1, 2);
表格中存储数据所编写的代码段,但没有任何内容存储。
user_story
我的帖子类型名称为{{1}} 参考:https://wordpress.stackexchange.com/questions/117269/saving-custom-post-in-custom-form/117331
答案 0 :(得分:0)
自定义帖子类型数据未存储,它是在运行时从register_post_type
args
提供的。
因此,当您使用它时,WP可能无法在前端或后端将数据与自定义帖子类型相关联。
WP_Query
用于类似帖子类型的功能。WP_List_Table
类显示数据。所以,如果你想要regester_custom_post_type
,你可以做一切框架,除了一些前端和数据库插入功能。
希望我已经澄清了关于URL,类别URL和后端数据的3个问题。
答案 1 :(得分:0)
1)设置自定义后期类型永久链接的最简单方法是使用相同名称的插件:https://wordpress.org/plugins/custom-post-type-permalinks/
它将以最小的麻烦完成您正在尝试做的事情。
2)您仍应在自定义插件或register_post_type
中使用functions.php
注册您的帖子类型。这至少会在DB中创建表格,您的前端表单会将内容存入。
3)您必须将前端表单上使用的下拉类别映射到作为自定义帖子类型一部分的数据库字段。对于自定义字段,我喜欢高级自定义字段插件。它将支持这种结构并正确地写入数据库,在模板层也很容易使用。
我可能还建议您查看WP-API功能套件。这将允许您使用一组程序化的挂钩从前端进入自定义后期类型。你现在正在做的事情很糟糕。