WordPress 的自定义文章类型(Post Type)是主题和插件开发中的核心功能,它允许你创建和管理不同于标准“文章”和“页面”的内容类型。本文将介绍如何创建自定义文章类型,并为其添加分类、自定义字段(Meta Box)以及在前端调用。
创建自定义文章类型
创建新的文章类型需要使用 register_post_type() 函数。通常,我们将此代码添加到主题的 functions.php 文件中。
function my_custom_post_product() {
$args = array();
register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );
上面的代码注册了一个名为“product”的文章类型,但参数为空,功能不完整。一个实用的例子通常需要配置标签(labels)和其他参数:
function my_custom_post_site() {
$labels = array(
'name' => _x( '网址导航', 'post type 名称' ),
'singular_name' => _x( '网址', 'post type 单个 item 时的名称' ),
'add_new' => _x( '新建网址', '添加新内容的链接名称' ),
'add_new_item' => __( '新建网址' ),
'edit_item' => __( '编辑网址' ),
'new_item' => __( '新网址' ),
'all_items' => __( '所有网址' ),
'view_item' => __( '查看网址' ),
'search_items' => __( '搜索网址' ),
'not_found' => __( '没有找到有关网址' ),
'not_found_in_trash' => __( '回收站里面没有相关网址' ),
'menu_name' => '网址导航'
);
$args = array(
'labels' => $labels,
'description' => '网址信息',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true
);
register_post_type( 'site', $args );
}
add_action( 'init', 'my_custom_post_site' );
将代码添加到 functions.php 后,刷新 WordPress 后台,你会在侧边栏看到新增的“网址导航”菜单项,这表示注册成功。
为自定义文章类型添加分类
使用 register_taxonomy() 函数可以为自定义文章类型添加分类(Taxonomy),其用法与注册文章类型类似,但需要指定关联的文章类型。
function my_taxonomies_site() {
$labels = array(
'name' => _x( '网址分类', 'taxonomy 名称' ),
'singular_name' => _x( '网址分类', 'taxonomy 单数名称' ),
'search_items' => __( '搜索网址分类' ),
'all_items' => __( '所有网址分类' ),
'parent_item' => __( '上级分类' ),
'parent_item_colon' => __( '上级分类:' ),
'edit_item' => __( '编辑网址分类' ),
'update_item' => __( '更新网址分类' ),
'add_new_item' => __( '添加新分类' ),
'new_item_name' => __( '新分类名称' ),
'menu_name' => __( '网址分类' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true, // 设为 true 则为分类模式,false 则为标签模式
'show_admin_column' => true // 在文章列表显示分类列
);
register_taxonomy( 'sitecat', 'site', $args );
}
add_action( 'init', 'my_taxonomies_site', 0 );
添加后,在“网址导航”的编辑页面就会出现自定义的“网址分类”元模块。
添加自定义字段(Meta Box)
自定义字段允许你为文章类型存储额外的信息。例如,为“网址”类型添加一个“网址链接”字段。
首先,使用 add_meta_box() 函数注册一个元模块:
add_action( 'add_meta_boxes', 'site_director' );
function site_director() {
add_meta_box(
'site_director', // 元模块 ID
'网址链接', // 元模块标题
'site_director_meta_box', // 回调函数,用于输出 HTML
'site', // 文章类型
'side', // 位置(side, normal, advanced)
'low' // 优先级
);
}
然后,定义回调函数来创建表单字段,并处理数据的保存:
// 输出表单字段
function site_director_meta_box($post) {
// 创建临时隐藏表单,为了安全
wp_nonce_field( 'site_director_meta_box', 'site_director_meta_box_nonce' );
// 获取之前存储的值
$value = get_post_meta( $post->ID, '_site_director', true );
echo '<label for="site_director">网址链接:</label>';
echo '<input type="text" id="site_director" style="width:100%" name="site_director" value="' . esc_attr( $value ) . '" placeholder="输入网址链接">';
}
// 保存数据
add_action( 'save_post', 'site_director_save_meta_box' );
function site_director_save_meta_box($post_id){
// 安全检查
if ( ! isset( $_POST['site_director_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['site_director_meta_box_nonce'], 'site_director_meta_box' ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['site_director'] ) ) {
return;
}
$site_director = sanitize_text_field( $_POST['site_director'] );
update_post_meta( $post_id, '_site_director', $site_director );
}
你还可以将自定义字段显示在文章列表页(All Posts)中:
// 在文章列表添加新列
add_filter("manage_edit-site_columns", "site_edit_columns");
function site_edit_columns($columns){
$columns['site_director'] = '网址链接';
return $columns;
}
// 在新列中显示数据
add_action("manage_posts_custom_column", "site_custom_columns");
function site_custom_columns($column){
global $post;
if ( $column == 'site_director' ) {
echo get_post_meta( $post->ID, '_site_director', true );
}
}
在前端调用数据
在主题模板文件中,你可以使用以下方式获取并显示自定义字段的值:
echo '网址链接:' . get_post_meta( get_the_ID(), '_site_director', true );
要查询并循环输出自定义文章类型的内容,可以使用 WP_Query:
$args = array(
'post_type' => 'site',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
the_title( '<h2>', '</h2>' );
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
wp_reset_postdata(); // 重置查询数据
else :
echo '暂无内容';
endif;
通过以上步骤,你可以创建功能完整的自定义文章类型,并灵活地管理和展示其内容。
按照文章里面的来复制,发现最后网站崩了,不知道为啥
你用的是什么主题,不一定适用于所有主题,可能主题本身已经有了冲突代码
只要添加 :自定义 Meta Box 需要用到 add_meta_box 函数
网站就会报错,其他的复制完都没问题
用的 the7 的主题