Create a custom event post

时间:2015-05-08 作者:Priyank Prajapati

我是一名WordPress学习者。我的问题是,如何在WordPress中创建自定义简单事件。你能简单地解释一下我该怎么做吗?我应该在哪里为自定义事件添加CSS和JS文件。

我也使用了这个链接,但它不起作用。noeltock.com

我应该使用哪种类型的钩子来创建自定义帖子类型?

1 个回复
SO网友:Jayson

在中创建自定义帖子类型的最简单方法WordPress 是通过使用插件。建议初学者使用此方法,因为它安全且简单。

例如:Custom Post Type UI

使用插件的问题是,当插件停用时,您的自定义帖子类型将消失。您可以通过在主题的functions.php 文件

// Our custom post type function
function create_posttype() {

    register_post_type( \'events\',
    // CPT Options
        array(
            \'labels\' => array(
                \'name\' => __( \'Event\' ),
                \'singular_name\' => __( \'Event\' )
            ),
            \'public\' => true,
            \'has_archive\' => true,
            \'rewrite\' => array(\'slug\' => \'events\'),
        )
    );
}
// Hooking up our function to theme setup
add_action( \'init\', \'create_posttype\' );
它使用参数数组注册post类型“events”。这些参数是自定义post类型的选项。这个数组有两部分,第一部分是标签,它本身就是一个数组。第二部分包含其他参数,如public visibility、has archive和slug。

/*
* Creating a function to create Custom post type
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Events\', \'Post Type General Name\', \'twentythirteen\' ),
        \'singular_name\'       => _x( \'Event\', \'Post Type Singular Name\', \'twentythirteen\' ),
        \'menu_name\'           => __( \'Events\', \'twentythirteen\' ),
        \'parent_item_colon\'   => __( \'Parent Event\', \'twentythirteen\' ),
        \'all_items\'           => __( \'All Events\', \'twentythirteen\' ),
        \'view_item\'           => __( \'View Event\', \'twentythirteen\' ),
        \'add_new_item\'        => __( \'Add New Event\', \'twentythirteen\' ),
        \'add_new\'             => __( \'Add New\', \'twentythirteen\' ),
        \'edit_item\'           => __( \'Edit Event\', \'twentythirteen\' ),
        \'update_item\'         => __( \'Update Event\', \'twentythirteen\' ),
        \'search_items\'        => __( \'Search Event\', \'twentythirteen\' ),
        \'not_found\'           => __( \'Not Found\', \'twentythirteen\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'twentythirteen\' ),
    );

// Set other options for Custom Post Type

    $args = array(
        \'label\'               => __( \'events\', \'twentythirteen\' ),
        \'description\'         => __( \'Event news and reviews\', \'twentythirteen\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        \'taxonomies\'          => array( \'types\' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );

    // Registering your Custom Post Type
    register_post_type( \'events\', $args );

}

/* Hook into the \'init\' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( \'init\', \'custom_post_type\', 0 );

结束

相关推荐

Only Showing Upcoming Events

在此页面的侧栏中:http://lifebridgecypress.org/our-people, 我有一个即将使用此代码的事件列表。。。<ul id=\"upcoming-events\"> <?php $latestPosts = new WP_Query(); $latestPosts->query(\'cat=3&showposts=10\'); ?> <?php while ($latestPos