最近,我一直在开发一个主题模板,以加快我的工作进度,并定制占用我大量时间的帖子类型。
以下是一些可能对您有所帮助的简单函数
function theme_template_create_post_type( $post_type, $labels = array(), $post_args = array() ) {
if ( !$labels || count( $labels ) < 10 ) {
$labels = array(
\'name\' => __( ucfirst( $post_type . \'s\' ) ),
\'singular_name\' => __( ucfirst( $post_type ) ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Add New\' ),
\'edit_item\' => __( \'Edit \' . ucfirst( $post_type ) ),
\'new_item\' => __( \'Add New\' ),
\'view_item\' => __( \'View \' . ucfirst( $post_type ) ),
\'search_items\' => __( \'Search \' . ucfirst( $post_type . \'s\' ) ),
\'not_found\' => __( \'No \' . $post_type . \'s found\' ),
\'not_found_in_trash\' => __( \'No \' . $post_type . \'s found in trash\' ),
\'all_items\' => __( \'All \' . ucfirst( $post_type . \'s\' ) )
);
}
$default_post_args = array( \'public\' => true, \'show_ui\' => true, \'capability_type\' => \'post\', \'hierarchical\' => false, \'menu_position\' => 20, \'supports\' => array( \'title\', \'editor\', \'thumbnail\' ) );
$post_args = wp_parse_args( $post_args, $default_post_args );
$custom_post_args = array(
\'labels\' => $labels,
\'public\' => $post_args[ \'public\' ],
\'show_ui\' => $post_args[ \'show_ui\' ],
\'capability_type\' => $post_args[ \'capability_type\' ],
\'hierarchical\' => $post_args[ \'hierarchical\' ],
\'menu_position\' => $post_args[ \'menu_position\' ],
\'supports\' => $post_args[ \'supports\' ],
\'rewrite\' => array( \'slug\' => $post_type )
);
register_post_type( $post_type, $custom_post_args );
}
function theme_template_create_updated_messages( $post_type ) {
add_filter( \'post_updated_messages\', function( $messages ) use( $post_type ) {
global $post, $post_ID;
$messages[ $post_type ] = array(
0 => \'\',
1 => sprintf( \'%s updated. <a href="%s">View %s</a>\', ucfirst( $post_type ), esc_url( get_permalink($post_ID) ), ucfirst( $post_type ) ),
2 => \'Custom field updated.\',
3 => \'Custom field deleted.\',
4 => sprintf( \'%s updated.\', ucfirst( $post_type ) ),
5 => isset($_GET[\'revision\']) ? sprintf( \'%s restored to revision from %s\', ucfirst( $post_type ), wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
6 => sprintf( \'%s published. <a href="%s">View %s</a>\', ucfirst( $post_type ), esc_url( get_permalink($post_ID) ), ucfirst( $post_type ) ),
7 => sprintf( \'%s saved.\', ucfirst( $post_type ) ),
8 => sprintf( \'%s submitted. <a target="_blank" href="%s">Preview %s</a>\', ucfirst( $post_type ), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ), ucfirst( $post_type ) ),
9 => sprintf( \'%3$s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %3$s</a>\', date_i18n( \'M j, Y @ G:i\', strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ), ucfirst( $post_type ) ),
10 => sprintf( \'%s draft updated. <a target="_blank" href="%s">Preview %s</a>\', ucfirst( $post_type ), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ), ucfirst( $post_type ) ),
);
return $messages;
});
}
function theme_template_create_post_title( $post_type, $post_title ) {
$custom_filter = function( $title ) use( $post_type, $post_title ) {
$screen = get_current_screen();
if ( $post_type == $screen->post_type ) {
$title = $post_title;
}
return $title;
};
add_filter( \'enter_title_here\', $custom_filter );
}
你可以这样称呼他们:
/*
* Custom post type [ slide ]
*/
add_action( \'init\', function() {
theme_template_create_post_type( \'slide\', array(), array( \'public\' => false, \'supports\' => array( \'title\', \'editor\' ) ) );
theme_template_create_updated_messages( \'slide\' );
theme_template_create_post_title( \'slide\', \'Enter slide name here\' );
});
Note you will require PHP Version 5.3.0 to use the anonymous function approach.
至于元框,我倾向于不采用代码优先的方法,因为我已经在那里做过,并且在路上遇到了很多错误/麻烦。(再说一遍,我不是世界上最好的WordPress开发人员,请原谅我)
我使用了一种叫做高级自定义字段的东西,可以在这里找到:http://wordpress.org/extend/plugins/advanced-custom-fields/ 免费版本(没有插件)满足了我的所有要求,它已经包含了我需要的许多功能!(同时看起来也很漂亮)
希望这有帮助:)