此代码设计为在主题和插件内部运行。适当的结构如下:
/my-theme
/cmb2
functions.php
index.php
screenshot.png
styles.css
快速启动
打开Example-functions.php并将所有代码复制/粘贴到functions.php
。创建一个新页面,您将在页面编辑器中看到所有示例元数据。使用GET_POST_META()获取/使用数据。
开始
首先,您需要获得引导并启动引擎。为此,请将以下代码添加到functions.php
。在插件或主题中包含CMB 2有一些警告。
注意:如果您正在安装插件WordPress.org,您可以跳过这个步骤,因为它是由插件处理的。
/**
* Get the bootstrap!
* (Update path to use cmb2 or CMB2, depending on the name of the folder.
* Case-sensitive is important on some systems.)
*/
require_once __DIR__ . '/cmb2/init.php';
注:
init.php
需要在任何钩子外。它需要尽早加载。- 不要做任何有条件的加载。
if ( ! class_exists....
。CMB 2将处理这个问题。
创建一个metabox(后台界面)
现在,您已经包含了CMB 2引擎,您可以开始使用下面的代码添加metabox了functions.php
:
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
/**
* Define the metabox and field configurations.
*/
function cmb2_sample_metaboxes() {
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
) );
// Regular text field
$cmb->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'yourprefix_text',
'type' => 'text',
'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
// 'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
// 'on_front' => false, // Optionally designate a field to wp-admin only
// 'repeatable' => true,
) );
// URL text field
$cmb->add_field( array(
'name' => __( 'Website URL', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'yourprefix_url',
'type' => 'text_url',
// 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols
// 'repeatable' => true,
) );
// Email text field
$cmb->add_field( array(
'name' => __( 'Test Text Email', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'yourprefix_email',
'type' => 'text_email',
// 'repeatable' => true,
) );
// Add other metaboxes as needed
}
注意:有关更多metabox示例,请参见Example-functions.php
使用插件创建metabox
您还可以通过创建一个标准WordPress插件并简单地将上面的代码粘贴到插件的标题下面来创建metabox。
显示元数据
最后,您需要能够提取元数据并使其工作。在主题或插件文件中,使用GET_POST_META()函数来显示元数据。记住,你必把身份证给我!
<?php
// Grab the metadata from the database
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
// Echo the metadata
echo esc_html( $text );
?>
让我们假设您使用上面概述的字段创建了一个metabox,并希望在单个页面模板上显示这一点:
<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
$email = get_post_meta( get_the_ID(), 'yourprefix_email', true );
$url = get_post_meta( get_the_ID(), 'yourprefix_url', true );
echo esc_html( $text );
echo is_email( $email );
echo esc_url( $url );
?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
有关使用POST META的更多信息,请参见Codex页面GET_POST_META()。记住,到转义任何和所有数据!使用适当数据验证使用POST META是正确的做法。不要相信任何人!
在用户配置文件中添加元数据
若要向“用户配置文件”页面添加自定义Metabox,可以将object_types
参数array( 'user' )
指示应该在用户配置文件(而不是特定的POST类型)上显示Metabox,并且应该根据user_meta而不是post_meta存储元信息。
在下面的例子中可以看到Metabox。Example-functions.php
注意Metaboxdescription
和name
参数将不显示。可以通过添加title
字段作为第一个字段。
关于捆绑和包含CMB 2的注意事项。
本节重点介绍一些包含CMB 2的工作/不需要做的事情。CMB 2有一种智能的方法,只加载自己的一个版本,只加载最新版本(我们还严格承诺向后兼容,这样更新的版本就不会破坏以前版本构建的代码)。如果你不适当地包括它,它可能会产生意想不到的后果。
- 正确:包括文件直接从你的主题或插件。例如:
require_once __DIR__ . '/includes/cmb2/init.php';
- 错误:包括钩子上的文件。例如:
// DON'T DO THIS add_action( 'init', 'wprpt_initialize_cmb_init', 10 ); function wprpt_initialize_cmb_init() { require_once __DIR__ . '/includes/cmb2/init.php'; }
- 正确:使用区分大小写的路径到包含文件.例如:
require_once __DIR__ . '/includes/CMB2/init.php';
或
require_once __DIR__ . '/includes/cmb2/init.php';
如果您从WordPress插件爬行下载CMB 2,它将是一个小写目录,
cmb2
。但是,由于Gizub的命名约定,如果您克隆了这个回购文件,或者下载了它,或者在版本页面上下载了一个版本,那么它将是一个大写目录,CMB2
。显然,当您将目录捆绑/包括在主题或插件中时,您可以任意命名目录。 - 错误:用
class_exists()
包括之前检查一下。// DON'T DO THIS if ( ! class_exists( 'Some_CMB2_Class' ) ) { require_once __DIR__ . '/includes/cmb2/init.php'; }
- 正确:使用适当的CMB 2包。作曲家是一种很好的推荐方法,但是如果要在主题/插件中捆绑CMB 2,请从WordPress插件安装.
- 错误:克隆回购到你的项目中。您将克隆所有的开发专用文件/文件夹。如果将主题提交到CMB 2捆绑的WordPress主题安装,你可能会被拒绝包括这些开发文件。
学习一下
混积分
混积分
牛
混积分
加油
谢谢
BANG !!!!
各位大佬不好意思,为了任务,抱歉了
CMB2是一个好框架
pgn r11