在WordPress中创建音乐人演唱会列表的最佳方式

时间:2012-02-28 作者:marctain

我目前正在设计一个音乐家的wordpress主题,并希望创建一个用户控制的动态演出列表。我在使用PHP之前就做过这件事,但我想知道在WordPress中做这件事的最佳方式是什么。

我已经测试了一种使用自定义帖子类型的方法,但我觉得有些用户可能会对为某个事件创建“帖子”,然后在底部的自定义字段中写入日期感到困惑。

如何让我的主题在wp数据库中创建自己的表?我是否可以避免这种情况,并制作自己的“插入gig”界面,创建自定义帖子类型,但使用更容易理解的“日期、地点、关于”方式输入信息?

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

创建一个名为“Gig”的自定义帖子类型,WordPress将负责UI的创建,然后只需添加一个包含所需字段(日期、地点、关于)的元数据库。

以下是一个简单的分步指南:

First: Register Your Gig Post Type

//register your custom post type gig
add_action( \'init\', \'register_cpt_gig\' );
function register_cpt_gig() {
    $labels = array( 
        \'name\'          => _x( \'gigs\', \'gig\' ),
        \'singular_name\' => _x( \'gig\', \'gig\' ),
        \'add_new\'       => _x( \'Add New\', \'gig\' ),
        \'add_new_item\'  => _x( \'Add New gig\', \'gig\' ),
        \'edit_item\'     => _x( \'Edit gig\', \'gig\' ),
        \'new_item\'      => _x( \'New gig\', \'gig\' ),
        \'view_item\'     => _x( \'View gig\', \'gig\' ),
        \'search_items\'  => _x( \'Search gigs\', \'gig\' ),
        \'not_found\'     => _x( \'No gigs found\', \'gig\' ),
        \'not_found_in_trash\' => _x( \'No gigs found in Trash\', \'gig\' ),
        \'parent_item_colon\'  => _x( \'Parent gig:\', \'gig\' ),
        \'menu_name\'     => _x( \'Gigs\', \'gig\' ),
    );

    $args = array( 
        \'labels\'        => $labels,
        \'hierarchical\'  => false,
        \'description\'   => \'just a simple "where, when, with who" kind of thing\',
        \'supports\'      => array( \'title\', \'custom-fields\' ),
        \'public\'        => true,
        \'show_ui\'       => true,
        \'show_in_menu\'  => true,
        \'menu_icon\'     => \'http://i.imgur.com/4nTMD.png\',
        \'show_in_nav_menus\'     => true,
        \'publicly_queryable\'    => true,
        \'exclude_from_search\'   => false,
        \'has_archive\'   => true,
        \'query_var\'     => true,
        \'can_export\'    => true,
        \'rewrite\'       => true,
        \'capability_type\'=> \'post\'
    );

    register_post_type( \'gig\', $args );
}
这将为您提供一个新的帖子类型和管理UI,用于创建您的Gig:

Gig Post Type Admin Page

Next you need to create a Metabox (避免使用常规自定义字段)

网上有一百万个关于如何创建和添加元数据库的教程,所以我不打算深入讨论,但我将向您展示一种快速简便的方法。Download this class 一旦你把这个放在你的主题中,Metabox的创建应该是这样的:

if( is_admin() ) {
    //include the main class file
    require_once( "meta-box-class/my-meta-box-class.php" );
    $prefix = \'_gigs\';

    //configure your meta box
    $config = array(
        \'id\'        => \'gigs-info\',
        \'title\'     => \'Gig Info\',
        \'pages\'     => array(\'gig\'),
        \'context\'   => \'normal\', 
        \'priority\'  => \'high\', 
        \'fields\'    => array(), 
        \'local_images\'   => false,
        \'use_with_theme\' => false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
    );
    $my_meta = new AT_Meta_Box( $config );

    //Add fields to your meta box
    $my_meta->addText( $prefix . \'where\', array( \'name\'=> \'Where is the Gig \' ) );
    $my_meta->addDate( $prefix . \'when\', array( \'name\'=> \'When is the Gig \' ) );
    $my_meta->addTime( $prefix . \'start_time\', array( \'name\'=> \'When Does it Start \' ) );
    $my_meta->addTime( $prefix . \'end_time\', array( \'name\'=> \'When Does it End \' ) );
    $my_meta->addText( $prefix . \'with_who\', array( \'name\'=> \'With Who is the Gig \' ) );
    $my_meta->addTextarea( $prefix . \'words\', array( \'name\'=> \'A few words on the gig \' ) );
    $my_meta->Finish();
}
您最终会得到类似的结果:

Add New Gig Admin Page

下面是另一幅展示奇特计时器的图片:

Gig Timepicker

有关可以使用并添加到元数据库的字段类型的详细信息read this.

就是这样!总而言之,我将此作为一个插件发布以进行测试,但请确保首先下载Metabox类,这样它才能工作。

<?php
/*
Plugin Name: wp-gigs
Plugin URI: http://en.bainternet.info
Description: create list of gigs.
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/

//register your custom post type gig
add_action( \'init\', \'register_cpt_gig\' );
function register_cpt_gig() {
    $labels = array( 
        \'name\'          => _x( \'gigs\', \'gig\' ),
        \'singular_name\' => _x( \'gig\', \'gig\' ),
        \'add_new\'       => _x( \'Add New\', \'gig\' ),
        \'add_new_item\'  => _x( \'Add New gig\', \'gig\' ),
        \'edit_item\'     => _x( \'Edit gig\', \'gig\' ),
        \'new_item\'      => _x( \'New gig\', \'gig\' ),
        \'view_item\'     => _x( \'View gig\', \'gig\' ),
        \'search_items\'  => _x( \'Search gigs\', \'gig\' ),
        \'not_found\'     => _x( \'No gigs found\', \'gig\' ),
        \'not_found_in_trash\' => _x( \'No gigs found in Trash\', \'gig\' ),
        \'parent_item_colon\'  => _x( \'Parent gig:\', \'gig\' ),
        \'menu_name\'     => _x( \'Gigs\', \'gig\' ),
    );

    $args = array( 
        \'labels\'        => $labels,
        \'hierarchical\'  => false,
        \'description\'   => \'just a simple "where, when, with who" kind of thing\',
        \'supports\'      => array( \'title\', \'custom-fields\' ),
        \'public\'        => true,
        \'show_ui\'       => true,
        \'show_in_menu\'  => true,
        \'menu_icon\'     => \'http://i.imgur.com/4nTMD.png\',
        \'show_in_nav_menus\'     => true,
        \'publicly_queryable\'    => true,
        \'exclude_from_search\'   => false,
        \'has_archive\'   => true,
        \'query_var\'     => true,
        \'can_export\'    => true,
        \'rewrite\'       => true,
        \'capability_type\' => \'post\'
    );

    register_post_type( \'gig\', $args );
}

if( is_admin() ) {
    //include the main class file
    require_once( "meta-box-class/my-meta-box-class.php" );
    $prefix = \'_gigs\';

    //configure your meta box
    $config = array(
        \'id\'        => \'gigs-info\',
        \'title\'     => \'Gig Info\',
        \'pages\'     => array( \'gig\' ),
        \'context\'   => \'normal\', 
        \'priority\'  => \'high\', 
        \'fields\'    => array(), 
        \'local_images\'  => false,
        \'use_with_theme\'=> false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
    );
    $my_meta = new AT_Meta_Box( $config );

    //Add fields to your meta box
    $my_meta->addText( $prefix . \'where\', array( \'name\'=> \'Where is the Gig \' ) );
    $my_meta->addDate( $prefix . \'when\', array( \'name\'=> \'When is the Gig \' ) );
    $my_meta->addTime( $prefix . \'start_time\', array( \'name\'=> \'When Does it Start \' ) );
    $my_meta->addTime( $prefix . \'end_time\', array( \'name\'=> \'When Does it End \' ) );
    $my_meta->addText( $prefix . \'with_who\', array( \'name\'=> \'With Who is the Gig \' ) );
    $my_meta->addTextarea( $prefix . \'words\', array( \'name\'=> \'A few words on the gig \' ) );
    $my_meta->Finish();
}

SO网友:Brian Fegter

我以前用过GigPress,效果非常好。

http://gigpress.com/

SO网友:Stephen Harris

我可以建议你Event Organiser?

全面披露:这是我开发的一个插件

它基本上实现了您想要实现的目标(创建自定义帖子类型),允许日期选择、地点和自定义字段,以便可以扩展以供您自己使用。

至于将其合并到主题中,插件附带了四个基本的“示例”模板。只需将这些内容复制到主题文件夹中,您就可以随心所欲地编辑它们,利用插件附带的模板功能来显示活动/场馆详细信息。

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register