这是我的第一个WordPress网站。 我正在创建一个当地的社区网站,我想拥有与Freecycle相似的功能,即用户可以免费为他人提供东西。因此,他们需要成为一个下拉盒,以说是想要的东西还是提供的物品。项目名称的标题字段。然后是该项目的详细信息。上传项目和位置字段的图像,人们必须从中捡起它。也可能是一个联系码字段。 另外,理想情况下,我想要一个复选框系统,以便如果将物品提供给某人,则用户可以勾选出提供的盒子,然后该帖子可能会被分配给等待收藏的类别,并且其文字会变灰并消失。帖子列表。 当收集到它时,他们可以单击另一个框,然后将其提供一个类别的存档。 我什至不知道是否有可能...
如果是,那么最好的方法是如何做到的?考虑到我希望能够在网站其他领域使用这些帖子,例如在侧边栏中,显示了最近提供的物品。我还希望能够添加将位于某些页面侧边栏中的表单的最小化版本,因此用户可以在其他页面上添加项目。
Http://codex.wordpress.org/function_reference/register_post_type(在WordPress开发方面,Codex是您最好的朋友!)
如果您仍然遇到麻烦,则有许多插件可以帮助创建自定义邮政类型,例如:http://wordpress.org/extend/plugins/plugins/custom-post-post-type-ui/ lastly,要在您的自定义帖子类型中添加自定义字段,我建议使用我经常使用的出色插件:
http://www.advancedcustomfields.com/(我发现它非常可靠,而且很棒节省时间,而不是手动创建自定义元字段) 我绝对建议您自己创建自定义帖子类型和自定义元字段 - 如果您没有时间在此构建上进行操作,那么请在下一个构建上腾出时间。很高兴知道如何手动做所有事情而不是依靠插件。
希望有帮助!
有两种方法:
PostType::create('custom_post_name')-> meta_box(array(
'id' => 'your_id',
'title' => 'your_title'
))->field(array(
'name' => 'your_field_name',
'label' => 'your field label',
'value' => 'your_field_default_value',
'type' => 'your_field_type'
));
function create_post_type() {
register_post_type( 'acme',
array(
'labels' => array(
'name' => __( 'acme' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'create_post_type' );
2。添加元箱
function add_custom_metabox(){
add_meta_box(
"your_id",
"Your Title",
"custom_metabox_content",
"post"
);
}
add_action("add_meta_boxes", "add_custom_metabox");
3。将字段添加到元框内容
function custom_meta_box_content($post_id){
$value = get_post_meta($post_id, "custom_meta_field", true);
echo '<input type="text" name="custom_meta_field" value="'. $value .'">';
}
4。收听以保存后挂钩和更新值(当您单击“管理面板”更新帖子时)。
function on_save_post($post_id){
$meta_value = isset($_POST["custom_meta_field"]) ? $_POST["custom_meta_field"] : false;
if (current_user_can("edit_posts") && $meta_value){
update_post_meta($post_id,"custom_meta_field", $meta_value);
}
}
add_action("save_post", "on_save_post");
在functions.php中使用此代码
function foobar_func( $atts )
{
global $post;
$key=$atts['name'];
if(empty($key)) return;
return get_post_meta($post->ID, $key, true);
}
add_shortcode( 'foobar', 'foobar_func' );
为显示
在您的帖子页面上。