当前位置:首页WP教程WordPress自定义文章类型(PostType)功能介绍

WordPress自定义文章类型(PostType)功能介绍

WordPress的自定义文章类型在开发一款WordPress主题的时候是经常会用到的,那么该如何创建一个新的自定义文章类型(PostType)呢?下面主题君给…

WordPress的自定义文章类型在开发一款WordPress主题的时候是经常会用到的,那么该如何创建一个新的自定义文章类型(PostType)呢?下面主题君给大家介绍下。

创建PostType

创建一个新的 PostType 需要使用 register_post_type 函数,在你主题的 functions.php 文件下调用该函数:

register_post_type( $post_type, $args );
//$post_type 参数就是你自定义 Post Type 的名称。
function my_custom_post_product() {
$args = array();
register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );

函数的参数很多,下面只列出比较常用的参数:

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' => __( '回收站里面没有相关网址' ),
'parent_item_colon' => '',
'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的最下面,进入后台你会发现多出了site选项,这样表示注册成功:

这时我们可以在新建的site 发表一篇网址类型的文章了。但是这样与文章类型基本相同,我们需要更多的自定义来完善我们的site类型。

添加分类功能需要使用函数register_taxonomy,使用方法也很简单,跟注册 Post Type 函数类似,只不过多了一个参数用来指定对应的 Post Type :

register_taxonomy( $taxonomy, $object_type, $args );

就本按理而言,可以配置如下常用参数:

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,);
register_taxonomy( 'sitecat', 'site', $args );
}
add_action( 'init', 'my_taxonomies_site', 0 );

添加到主题之后,我们看到出现了熟悉的文章分类功能,只不过上面的文案全部变成我们自定义的内容了:

为 Post Type 添加自定义 Meta Box

我们想要添加的网址导航类型不能仅仅只有文章内容,我们还需要额外添加一些 网址链接 之类的有关内容。那么就需要添加自定义 Meta Box,Meta Box 可以在文章发表页面中添加自定义的表单,编写文章的时候可以填写额外的信息然后在前端调用出来。

自定义 Meta Box 需要用到add_meta_box函数:

add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );
add_action( 'add_meta_boxes', 'site_director' );
function site_director() {
add_meta_box('site_director','网址链接','site_director_meta_box','site','side','low');
}

接下来注册一个 Meta Box :

然后在配置参数里面指定了回调函数site_director_meta_box,我们需要在这个函数里面创建表单:

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 );
?>
<label for="site_director"></label>
<input type="text" id="site_director" name="site_director" value="<?php echo esc_attr( $value ); ?>" placeholder="输入网址链接" >
<?php
}
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;
}
// 判断 Meta Box 是否为空
if ( ! isset( $_POST['site_director'] ) ) {
return;
}
$site_director = sanitize_text_field( $_POST['site_director'] );
update_post_meta( $post_id, '_site_director', $site_director );
}

添加自定义字段:

add_action("manage_posts_custom_column", "site_custom_columns");
add_filter("manage_edit-site_columns", "site_edit_columns");
function site_custom_columns($column){
global $post;
switch ($column) {
case "site_director":
echo get_post_meta(
$post->ID, '_site_director', true
);
break;
}
}
function site_edit_columns($columns){
$columns['site_director'] = '网址';
return $columns;
}

显示 Meta Box 内容

echo '网址:'.get_post_meta( get_the_ID(), '_site_director', true );

实用 WP_Query 高度自定义调用 Post Type 的内容:

$args = array(
'post_type' => 'site',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div>';
the_content();
echo '</div>';
endwhile;

给TA打赏
共{{data.count}}人
人已打赏
WP教程

WordPress禁止古腾堡编辑器加载谷歌字体

2023-3-8 22:16:27

WP教程

WordPress怎么给文章外链添加nofollow

2023-3-8 22:16:36

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
有新私信 私信列表
搜索