从前端添加自定义帖子类型项目

时间:2012-11-03 作者:Sze Chuan

我想知道如何让我的wordpress网站能够添加portfolio 来自前端的项目。诸如WP User Frontend之类的插件允许从前端发帖,但只能用于博客帖子。

是否有任何方法或插件类似于wp user frotend,但能够添加自定义帖子类型的项目?

2 个回复
最合适的回答,由SO网友:Adam 整理而成

如果您愿意付费,Gravity表单插件允许您创建映射到自定义帖子类型(甚至常规帖子和页面类型)以及映射到自定义字段的表单。

对于那些不愿意或愿意卷起袖子的人,您可以创建一个前端表单,将数据发布到您选择的任何帖子类型中。

这是一个基本的例子;

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

//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title     = $_POST[\'title\'];
$content   = $_POST[\'content\'];
$post_type = \'my_custom_post\';
$custom_field_1 = $_POST[\'custom_1\'];
$custom_field_2 = $_POST[\'custom_2\'];    

//the array of arguements to be inserted with wp_insert_post
$new_post = array(
\'post_title\'    => $title,
\'post_content\'  => $content,
\'post_status\'   => \'publish\',          
\'post_type\'     => $post_type 
);

//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
$pid = wp_insert_post($new_post);

//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, \'meta_key\', $custom_field_1, true);
add_post_meta($pid, \'meta_key\', $custom_field_2, true);

}
您的HTML表单看起来类似于;

<form method="post" name="front_end" action="" >
<input type="text" name="title" value="My Post Title" />
<input type="text" name="content" value="My Post Content" />
<input type="text" name="custom_1" value="Custom Field 1 Content" />
<input type="text" name="custom_2" value="Custom Field 2 Content" />
<button type="button">Submit</button>
<input type="hidden" name="action" value="my_post_type" />
</form>
您可以将这些内容全部放入主题模板文件中。通常,我会更进一步,从函数中的函数运行处理逻辑(PHP)。php钩住了一个动作,但它也可以在一个主题文件中工作。

这只是一个基本的例子,没有任何serious 错误检查和验证。然而,这为您提供了从前端发布到后端发布类型所需内容的基本框架。

WPSE上也有许多关于该主题的教程,如果您运行搜索,您将发现大量信息。

SO网友:Aakib

<?php 

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

//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title     = $_POST[\'title\'];
$content   = $_POST[\'content\'];
$tags   = $_POST[\'tag\'];
$custom_field = $_POST[\'custom_1\']; 
$post_type = \'frontpost\';


//the array of arguements to be inserted with wp_insert_post
$new_post = array(
\'post_title\'    => $title,
\'post_content\'  => $content,
\'tags_input\'  => $tags,
\'post_status\'   => \'publish\',
\'post_category\' => array(\'0\',$_POST[\'cat\']),          
\'post_type\'     => $post_type 
);

//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
//we now use $pid (post id) to help add out post meta data
 $pid=wp_insert_post($new_post);

//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, \'cust_key\', $custom_field);


}
?>

    <div class="front-form col-sm-6">
        <form method="post" name="front_end" action="" >
            <input type="text" name="title" placeholder="FrontPost Title" required />
             <textarea  name="content" placeholder="FrontPost Content" rows="5" ></textarea>
            <input type="text" name="tag" placeholder="FrontPost tags" />
            <input type="text" name="custom_1" placeholder="Custom Field  Content" />


            <span><?php wp_dropdown_categories( \'tab_index=10&taxonomy=category&hide_empty=0\' ); ?></span>
            <button type="submit">Submit</button>
            <input type="hidden" name="action" value="front_post" />
        </form>
    </div>

试试这个,它会帮你的。。!Aakib:)

结束

相关推荐

Front-End Post Submission

我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。