我是新來的WordPress如此道歉,如果我的問題看起來很愚蠢。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
功能這篇文章類型這些職位?我在形式類別的下拉。我如何告訴WordPress將其用作子鏈接並加載與此類別相關的帖子?
我很抱歉在一篇文章中提出了多個問題。我只想把它們放在一個地方,因爲它們看起來是相關的。
UPDATE
我已經丟棄存儲數據我在做遠路的想法,並通過與register_post_type
功能註冊它創造了一個custom 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
。
參考:https://wordpress.stackexchange.com/questions/117269/saving-custom-post-in-custom-form/117331
如果您認爲不然,請告訴我。我很想學習。 –
就存儲和檢索數據而言,表單工作正常。我已經使用'WP_Query'在列表頁面中爲'post_type =>'story''獲取數據。但是我不知道如何在不使用函數的情況下使用自定義重寫模式。其實我試圖用'meta_box'創建一個表單,並且通過註冊帖子類型,但是我無法弄清楚,因爲我對WP還是一個新手。如果你請給我一個關於這個的教程鏈接,這將大大幫助我。 –
如何將使用'register_custom_post_type'創建的相同自定義帖子類型條目表單帶到前端?將「shortcode」做到這一點嗎?如果是,當我保存數據時,我應該使用'wp_insert_post',這會將元數據自動放入'wp_postmeta'表中?我搜索了這個,但無法找到適當的答案。你介意請稍微解釋一下嗎? –