【问题标题】:WordPress: Custom Post Type Permalink taking me back to homepageWordPress:自定义帖子类型永久链接带我回到主页
【发布时间】:2017-06-26 02:07:55
【问题描述】:

我是 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 帖子类型使用特定模式。有没有办法在上面的代码中定义?

我可以再问几件事吗?

  1. 由于我没有使用register_post_type 函数注册此帖子类型,如何在wp-admin 内查看这些帖子?

  2. 我在表单中有一个类别下拉列表。如何告诉 WordPress 将此用作子链接并加载与此类别相关的帖子?

很抱歉在一篇帖子中提出了多个问题。我只想将它们放在一个地方,因为它们看起来是相关的。

更新
我已经放弃了像以前那样存储数据的想法,并通过使用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');

下面是我编写的代码 sn-p,用于将数据存储在 wp_postswp_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

【问题讨论】:

    标签: wordpress custom-post-type permalinks


    【解决方案1】:

    不存储自定义帖子类型数据,它是在运行时从register_post_type args 提供的添加的。

    因此,当您使用它时,WP 可能无法在前端或后端将数据与自定义帖子类型相关联。

    1. 如果你不想使用那个函数,你必须自己写 WP_Query 用于类似帖子类型的功能。
    2. 另外,创建自定义后端菜单并在其上运行这些查询以 使用WP_List_Table 类显示数据。
    3. 最后,URL 路由必须单独处理。

    所以,如果您避免使用 regester_custom_post_type,那么除了一些前端和数据库插入功能之外,您可以完成框架所做的所有事情。

    希望我已经澄清了您关于 URL、类别 URL 和后端数据的 3 个问题。

    【讨论】:

    • 就存储和检索数据而言,该表单工作正常。我已经在使用WP_Query 在我的列表页面中获取post_type=&gt;'story' 的数据。但我不知道如何在不使用该功能的情况下使用自定义重写模式。实际上,我尝试使用meta_box 并通过注册帖子类型来创建一个表单,但由于我还是 WP 的新手,所以我无法弄清楚。如果你请给我一个关于这个的教程链接,这将对我有很大帮助。
    • 如何将使用register_custom_post_type 创建的自定义帖子类型输入表单带到前端? shortcode 会这样做吗?如果是,当我保存数据时,我应该使用wp_insert_post,这会自动将元数据放入wp_postmeta 表中吗?我对此进行了谷歌搜索,但找不到正确的答案。你介意解释一下吗?
    【解决方案2】:

    1) 设置自定义帖子类型永久链接的最简单方法是使用同名插件:https://wordpress.org/plugins/custom-post-type-permalinks/

    它会以最少的麻烦完成您想要做的事情。

    2) 您仍应在自定义插件中使用register_post_typefunctions.php 注册您的帖子类型。这至少会在您的前端表单将内容存入的数据库中创建表。

    3) 您必须将前端表单上使用的下拉类别映射到作为自定义帖子类型一部分的数据库字段。对于自定义字段,我喜欢 Advanced Custom Fields 插件。它将支持这种结构并正确写入数据库,在模板层也很容易使用。

    我还建议您查看 WP-API 功能套件。这将允许您使用一组程序化的挂钩来从前端接入自定义的帖子类型。你现在正在做的事情很 hacky。

    【讨论】:

    • 感谢您的建议。我同时放弃了以我的方式保存自定义帖子类型数据的想法。相反,我现在使用 register_post_type 函数创建了一个自定义帖子类型,该函数将在管理员中出现。顺便说一句,我已经在我的其他两个 CPT 中使用 ACF。因为我正在学习 WP,所以我想自己创建一个。在我的新 CPT 中,我创建了三个 CM Box,并且可以使用 shortcode 在前端页面中显示表单。但在这里我迷路了。浏览了一些文章,但无法将数据保存在 wp_postswp_postmeta 表中。在下一条评论中添加了更多信息。
    • 我写了一个函数render_user_story_form,它输出表单。然后编写了另一个函数save_user_story,它应该保存数据。添加了add_action('save_post_user_story', 'save_user_story', 1, 2); 来存储数据。我一定在这里做错了。我用save_user_story 函数更新了我的问题。请指教。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2014-05-17
    • 2013-09-10
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    相关资源
    最近更新 更多