自定义发布类型-如何包括自定义域

时间:2018-07-13 作者:Yussuf

我对wordpress和内容管理系统相当陌生。我已经开始学习了一些教程。我一直在搜索如何在添加新的自定义帖子类型中包含日期选择器。

My Events Custom Post Type

我想在这里添加一个额外的字段,以便用户选择一个日期来指定事件发生的时间。我不希望用户通过文本手动键入日期,但希望使用日期选择器,无论是普通的html5日期选择器还是jquery日期选择器。

我用来生成它的代码在函数中。php,我知道这可能不是放置我所有代码的最佳位置,但我目前正在尝试,但似乎找不到解决问题的方法。

    /*
    Custom post types
*/

function awesome_custom_post_type ()
{
    $labels = array(
        \'name\' => \'Events\',
        \'singular_name\' => \'Event\',
        \'add_new\' => \'Add Event\',
        \'all_items\' => \'All Events\',
        \'add_new_item\' => \'Add Event\',
        \'edit_item\' => \'Edit Event\',
        \'new_item\' => \'New Event\',
        \'view_item\' => \'View Event\',
        \'search_item_label\' => \'Search Events\',
        \'not_found\' => \'No Events Found\',
        \'not_found_in_trash\' => \'No Events Found in Trash\',
        \'parent_item_colon\' => \'Parent Event\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'publicly_queryable\' => false,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'thumbnail\',
        ),
        \'menu_icon\' => \'dashicons-calendar-alt\',
        \'menu_position\' => 5,
        \'excluse_from_search\' => true
    );

    register_post_type( \'awesome_events\', $args );
}
add_action( \'init\', \'awesome_custom_post_type\' );
提前谢谢。

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

在苏尼尔的回答的帮助下,我确切地知道了我需要做什么。

我去下载了免费版本的Metaboxhttps://wordpress.org/plugins/meta-box/

一旦我下载了它,我就把它放在child-theme/external/meta-box并引用自child-theme/inc/events-custom-post-type.php.

events-custom-post-type.php 看起来像这样。

require_once(get_stylesheet_directory() . \'/external/meta-box/meta-box.php\');

function custom_events ()
{
    $labels = array(
        \'name\' => \'Events\',
        \'singular_name\' => \'Event\',
        \'add_new\' => \'Add Event\',
        \'all_items\' => \'All Events\',
        \'add_new_item\' => \'Add Event\',
        \'edit_item\' => \'Edit Event\',
        \'new_item\' => \'New Event\',
        \'view_item\' => \'View Event\',
        \'search_item_label\' => \'Search Events\',
        \'not_found\' => \'No Events Found\',
        \'not_found_in_trash\' => \'No Events Found in Trash\',
        \'parent_item_colon\' => \'Parent Event\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'publicly_queryable\' => false,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'supports\' => array(
            \'title\',
            \'editor\',
        ),
        \'menu_icon\' => \'dashicons-calendar-alt\',
        \'menu_position\' => 5,
        \'exclude_from_search\' => true
    );

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

function prefix_register_meta_boxes_events( $meta_boxes ) {
    $prefix = \'custom_event_\';

    $meta_boxes[] = array(
        \'id\'         => $prefix . \'details\',
        \'title\'      => \'Event details\',
        \'post_types\' => \'custom_events_post_type\',
        \'context\'    => \'normal\',
        \'priority\'   => \'high\',

        \'fields\' => array(
            array(
                \'name\'  => \'Event date\',
                \'desc\'  => \'Select event date\',
                \'id\'    => $prefix . \'date\',
                \'type\'  => \'date\',
            ),
            array (
                \'name\' => \'Event location\',
                \'desc\' => \'Location of the event\',
                \'id\'   => $prefix . \'location\',
                \'type\' => \'text\'
            )
        )
    );

    return $meta_boxes;
}

add_action( \'init\', \'custom_events\' );

add_filter( \'rwmb_meta_boxes\', \'prefix_register_meta_boxes_events\' );
Thecustom_events 函数设置自定义帖子类型,而prefix_register_meta_boxes_events 函数设置自定义字段日期选择器。

你只需要确保prefix_register_meta_boxes_events 函数,其中显示post_types 添加您自己的自定义帖子类型注册为的内容。

之后,我在中引用了此文件child-theme/functions.php 就像这样。

require get_stylesheet_directory() . \'/inc/events-custom-post-type.php\';

SO网友:sunil

@维穆兹

您需要将js和css文件排队到admin\\u print\\u脚本和admin\\u print\\u样式

下面是如何做到这一点的示例

// Register datepicker ui for properties

function admin_homes_for_sale_javascript()
{
    global $post;
    if($post->post_type == \'homes-for-sale\' && is_admin()) {
        wp_enqueue_script(\'jquery-ui-datepicker\', WP_CONTENT_URL . \'/themes/yourthemename/js/jquery-ui-datepicker.min.js\');  
    }
}
add_action(\'admin_print_scripts\', \'admin_homes_for_sale_javascript\');

// Register ui styles for properties

function admin_homes_for_sale_styles(){
    global $post;
    if($post->post_type == \'homes-for-sale\' && is_admin()) {
        wp_enqueue_style(\'jquery-ui\', WP_CONTENT_URL . \'/themes/yourthemename/css/jquery-ui-1.8.11.custom.css\');  
    }
}
add_action(\'admin_print_styles\', \'admin_homes_for_sale_styles\');
或者试试这个https://en.bainternet.info/how-i-add-a-wordpress-metabox/#toc-dn

结束

相关推荐

Wordpress updates and Git

使用git时如何处理wordpress核心和插件更新?我需要更新我的核心和插件。我在本地主机上有站点,在服务器上有实时站点。如果我更新localy,然后提交+推送更改,则会破坏站点,因为实时数据库未更新。若我更新了实时站点,我对localhost并没有任何更改,也并没有办法测试更新是否破坏了站点。