CMB2 Attached Posts 项目教程
1. 项目的目录结构及介绍
CMB2 Attached Posts 项目的目录结构如下:
cmb2-attached-posts/
├── css/
│ └── attached-posts-field.css
├── js/
│ └── attached-posts-field.js
├── attached-posts-field.php
├── composer.json
├── example-field-setup.php
├── init.php
├── LICENSE
├── README.md
├── attached-posts-field.png
└── attached-posts-search.gif
目录结构介绍
css/
: 包含项目的样式文件。js/
: 包含项目的JavaScript文件。attached-posts-field.php
: 核心文件,定义了附加帖子字段的功能。composer.json
: Composer 配置文件,用于依赖管理。example-field-setup.php
: 示例文件,展示了如何设置附加帖子字段。init.php
: 初始化文件,用于项目的初始化设置。LICENSE
: 项目的许可证文件。README.md
: 项目的说明文档。attached-posts-field.png
: 项目截图。attached-posts-search.gif
: 项目功能演示动画。
2. 项目的启动文件介绍
项目的启动文件是 init.php
。该文件负责初始化 CMB2 Attached Posts 插件,并确保其正确加载和运行。
init.php
文件内容概述
<?php
/**
* Initialize the CMB2 Attached Posts Field.
*/
require_once 'attached-posts-field.php';
该文件通过 require_once
引入了 attached-posts-field.php
文件,确保核心功能文件被加载。
3. 项目的配置文件介绍
项目的配置文件主要是 composer.json
和 example-field-setup.php
。
composer.json
文件内容概述
{
"name": "cmb2/cmb2-attached-posts",
"description": "Custom field for CMB2 for attaching posts to a page",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "CMB2 Team",
"email": "cmb2@example.com"
}
],
"require": {}
}
该文件定义了项目的名称、描述、许可证和作者信息。require
部分用于声明项目的依赖。
example-field-setup.php
文件内容概述
<?php
/**
* Example of how to set up a CMB2 field for attaching posts.
*/
add_action( 'cmb2_admin_init', 'cmb2_attached_posts_field_metabox' );
function cmb2_attached_posts_field_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'attached_posts_metabox',
'title' => __( 'Attached Posts', 'cmb2' ),
'object_types' => array( 'page' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$cmb->add_field( array(
'name' => __( 'Attached Posts', 'cmb2' ),
'desc' => __( 'Drag posts from the left column to the right column to attach them to this page.', 'cmb2' ),
'id' => 'attached_posts',
'type' => 'attached_posts',
'options' => array(
'query_args' => array(
'post_type' => 'post', // Post type
),
),
) );
}
该文件展示了如何设置 CMB2 附加帖子字段,包括如何创建元框和添加字段。
以上是 CMB2 Attached Posts 项目的详细教程,涵盖了项目的目录结构、启动文件和配置文件的介绍。希望对您有所帮助!