【问题标题】:Wordpress Form custom fieldWordpress 表单自定义字段
【发布时间】:2012-03-30 01:50:03
【问题描述】:

我使用 wordpress 创建了一个返回标题和描述的表单。我需要返回更多自定义字段。我在互联网上搜索过,发现很多答案都没有很好地解释它,他们最终没有为我工作。该表单将返回包含标题、描述和已填写的自定义字段的帖子。谢谢!

if('POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter some notes';
}


// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title'    =>  $title,
'post_content'  =>  $description,
'post_Blog_URL' => $URL,  
'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
'post_type' =>  'post'  //'post',page' or use a custom post type if you want to
);

add_post_meta($post_id, $meta_key, 'URL' , $unique); 

//SAVE THE POST
$pid = wp_insert_post($new_post);

//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );

} // 结束整个表单的 IF 语句

do_action('wp_insert_post', 'wp_insert_post');

提交您自己的!

                <div id="postbox">

                <form id="new_post" name="new_post" method="post" action="" class="" enctype="multipart/form-data">

                <!-- post name -->
                <fieldset name="name">
                    <label>Name of the Article</label>
                    <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
                </fieldset>   


                <!-- post Content -->
                <fieldset class="content">
                    <label for="description">Description</label>
                    <textarea id="description" tabindex="15" name="description"></textarea>
                </fieldset>


                <button type="submit">Submit</button>

                <input type="hidden" name="action" value="new_post" />

                <?php wp_nonce_field( 'new-post' ); ?>

                </form>

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    如果我理解正确,您希望能够在表单中显示帖子元字段。

    有一些功能可以设置、更新和检索这些,这将很有帮助:

    get_post_metaget_post_custom

    add_post_meta

    update_post_meta

    使用这些功能,您可以为帖子创建任何类型的元信息,并根据场景在管理面板或最终用户中显示它们。

    【讨论】:

    • 是的,我查看了所有这些功能,但我在实现中遇到了问题。情况:来自前端的 sser 将通过自定义表单提交一个字段,然后我需要将其输入到帖子中,进入自定义字段。我需要改变我的功能吗?还是只是post php?
    • 您需要使用 WordPress 钩子让它在表单保存时调用这些函数。您的 PHP 表明它已经在使用 URL 元数据执行此操作。您或多或少只是想扩展正在执行的操作以添加新字段。
    • 准确地说,我只是不确定该怎么做。描述和标题已经内置在 wordpress 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2017-07-30
    • 2017-11-05
    相关资源
    最近更新 更多