一文搞懂wordpress新增自定义文章类型和新增自定义分类法

本文关键词:

wordpree自定义文章类型,wordpress自定义分类法,wordpress新增自定义文章类型,wordpress新增自定义分类法

前言

我是kekc,一个PHP爱好者,也是一个wordpress爱好者,喜欢折腾,喜欢分享原创,欢迎大家礼貌转载。本人开发过wordpress插件、主题。在开发过程中,我经常新增自定义文章类型和自定义分类法。之前也曾分享过类似的代码,但是在这里我进行了一下封装。至于什么是自定义文章类型,就是新增一个像wordpress文章一样;至于什么是分类法,就像wordpress文章下面的分类、标签一样。网上大部分将新建wordpress分类法叫做新建wordpress分类,我现在认为是不正确的,我而且在我之前的代码分享中,也是叫的分类。

由于我经常使用,所以就简单封装了一下,让大家简单、快速的新增自己的自定义文章类型和自定义分类法。

我封装的代码如下:

<?php
/*
 * Author: KEKC
 * Author URI: https://www.kekc.cn
 */

/**
 * 注册自定义文章类型
 */
function custom_post_init() {
//预置参数
$custom_post_type_array = array(//注册自定义文章类型
    array(
      "post_type_Name" => '文章类型一',//文章名称
      "post_type_NameEn" =>'posttype1'//文章英文名,不能含有大写字母
    ),
    array(
      "post_type_Name" => '文章类型二',
      "post_type_NameEn" =>'posttype2'
    ),
  );
$taxArray = array(//注册自定义分类法
    array(
      "taxName" => '分类一',//分类法名称
      "taxNameEn" =>'taxSlu1',//分类法英文名
      "post_type" =>'posttype1',//挂载到所属的文章类型,对应post_type_NameEn,默认的是post
    ),
    array(
      "taxName" => '分类二',
      "taxNameEn" =>'taxSlu2',
      "post_type" =>'posttype2',
    ),
  );
 //预置参数
 
 



foreach ($custom_post_type_array as $custom_post_type) {//新建自定义文章类型
    $labels = array(
        'name'                    => _x( $custom_post_type['post_type_Name'], 'Post type general name', 'textdomain' ),
        'singular_name'           => _x( $custom_post_type['post_type_Name'], 'Post type singular name', 'textdomain' ),
        'menu_name'               => _x( $custom_post_type['post_type_Name'], 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'          => _x( $custom_post_type['post_type_Name'], 'Add New on Toolbar', 'textdomain' ),
        'add_new'                 => __( '新增', 'textdomain' ),
        'add_new_item'            => __( '添加新'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'new_item'                => __( '新'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'edit_item'               => __( '修改'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'view_item'               => __( '查看'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'all_items'               => __( '所有'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'search_items'            => __( '搜素'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'parent_item_colon'       => __( ''.$custom_post_type['post_type_Name'].'的上级:', 'textdomain' ),
        'not_found'               => __( '没有找到'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'not_found_in_trash'      => __( '没有在回收站中找到'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'featured_image'          => _x( '特色图片', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'      => _x( '设置特色图片', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image'   => _x( '移除特色图片', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'      => _x( '用作特色图片', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'                => _x( $custom_post_type['post_type_Name'].'归档', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'        => _x( '插入到'.$custom_post_type['post_type_Name'], 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item'   => _x( '上传至'.$custom_post_type['post_type_Name'], 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'       => _x( '筛选'.$custom_post_type['post_type_Name'].'列表', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation'   => _x( $custom_post_type['post_type_Name'].'列表导航', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'              => _x( $custom_post_type['post_type_Name'].'列表', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
        'item_published'          => $custom_post_type['post_type_Name']."已发布。",
        'item_published_privately'=>$custom_post_type['post_type_Name']."已私密发布。",
        'item_reverted_to_draft'  =>$custom_post_type['post_type_Name']."已恢复为草稿。",
        'item_scheduled'          =>$custom_post_type['post_type_Name']."已排入发布计划。",
        'item_updated'            =>$custom_post_type['post_type_Name']."已更新。",
        'item_link'               =>$custom_post_type['post_type_Name']."链接。",
        'item_link_description'   =>"目标".$custom_post_type['post_type_Name']."的链接。"
        
        
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => $custom_post_type['post_type_NameEn'] ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );
    register_post_type( $custom_post_type['post_type_NameEn'], $args );
}




foreach ($taxArray as $tax) {//新建自定义 tax 分类法
    $labels = array(
        'name'              => _x( $tax['taxName'], 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( $tax['taxName'], 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( '搜素'.$tax['taxName'], 'textdomain' ),
        'all_items'         => __( '所有'.$tax['taxName'], 'textdomain' ),
        'view_item'         => __( '查看'.$tax['taxName'], 'textdomain' ),
        'parent_item'       => __( $tax['taxName'].'上级', 'textdomain' ),
        'parent_item_colon' => __( $tax['taxName'].'的上级:', 'textdomain' ),
        'edit_item'         => __( '修改'.$tax['taxName'], 'textdomain' ),
        'update_item'       => __( '更新'.$tax['taxName'], 'textdomain' ),
        'add_new_item'      => __( '添加新的'.$tax['taxName'], 'textdomain' ),
        'new_item_name'     => __( '新的'.$tax['taxName'].'名称', 'textdomain' ),
        'not_found'         => __( '没有找到相关的'.$tax['taxName'], 'textdomain' ),
        'back_to_items'     => __( '返回'.$tax['taxName'], 'textdomain' ),
        'menu_name'         => __( $tax['taxName'], 'textdomain' ),
    );
 
    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'public'            => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
        'show_in_rest'      => true,
    );
    register_taxonomy( $tax['taxNameEn'].'taxonomy', $tax['post_type'], $args );
}


}
 
add_action( 'init', 'custom_post_init' );

怎么自定义,主要是修改上方的预置参数,也就是下面的:

//预置参数
$custom_post_type_array = array(//注册自定义文章类型
    array(
      "post_type_Name" => '文章类型一',//文章名称
      "post_type_NameEn" =>'posttype1'//文章英文名,不能含有大写字母
    ),
    array(
      "post_type_Name" => '文章类型二',
      "post_type_NameEn" =>'posttype2'
    ),
  );
$taxArray = array(//注册自定义分类法
    array(
      "taxName" => '分类一',//分类名称
      "taxNameEn" =>'taxSlu1',//分类英文名
      "post_type" =>'posttype1',//挂载到所属的文章类型,对应post_type_NameEn,默认的是post
    ),
    array(
      "taxName" => '分类二',
      "taxNameEn" =>'taxSlu2',
      "post_type" =>'posttype2',
    ),
  );
 //预置参数

如何使用(两种方式):

1、将修改好的代码放入主题文件夹下functions.php

2、将修改好的代码设置为插件,示例如下:

将下方代码新建文件,保存至wordpress插件目录,比如我的我保存到了/wp-content/plugins/nav_type_posts/index.php

<?php  
/*
 * Plugin Name: 注册自定义文章类型、自定义分类
 * Plugin URI: https://www.dift.cn
 * Description: 添加自定义的导航类文章类型和分类
 * Version: 1.0.0
 * Author: KEKC
 * Author URI: https://www.kekc.cn
 */
//你的代码放在下面
/**
 * 注册自定义文章类型
 */
function custom_post_init() {
//预置参数
$custom_post_type_array = array(//注册自定义文章类型
    array(
      "post_type_Name" => '文章类型一',//文章名称
      "post_type_NameEn" =>'posttype1'//文章英文名,不能含有大写字母
    ),
    array(
      "post_type_Name" => '文章类型二',
      "post_type_NameEn" =>'posttype2'
    ),
  );
$taxArray = array(//注册自定义分类
    array(
      "taxName" => '分类一',//分类名称
      "taxNameEn" =>'taxSlu1',//分类英文名
      "post_type" =>'posttype1',//挂载到所属的文章类型,对应post_type_NameEn,默认的是post
    ),
    array(
      "taxName" => '分类二',
      "taxNameEn" =>'taxSlu2',
      "post_type" =>'posttype2',
    ),
  );
 //预置参数
 
 



foreach ($custom_post_type_array as $custom_post_type) {//新建自定义文章类型
    $labels = array(
        'name'                    => _x( $custom_post_type['post_type_Name'], 'Post type general name', 'textdomain' ),
        'singular_name'           => _x( $custom_post_type['post_type_Name'], 'Post type singular name', 'textdomain' ),
        'menu_name'               => _x( $custom_post_type['post_type_Name'], 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'          => _x( $custom_post_type['post_type_Name'], 'Add New on Toolbar', 'textdomain' ),
        'add_new'                 => __( '新增', 'textdomain' ),
        'add_new_item'            => __( '添加新'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'new_item'                => __( '新'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'edit_item'               => __( '修改'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'view_item'               => __( '查看'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'all_items'               => __( '所有'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'search_items'            => __( '搜素'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'parent_item_colon'       => __( ''.$custom_post_type['post_type_Name'].'的上级:', 'textdomain' ),
        'not_found'               => __( '没有找到'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'not_found_in_trash'      => __( '没有在回收站中找到'.$custom_post_type['post_type_Name'], 'textdomain' ),
        'featured_image'          => _x( '特色图片', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'      => _x( '设置特色图片', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image'   => _x( '移除特色图片', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'      => _x( '用作特色图片', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'                => _x( $custom_post_type['post_type_Name'].'归档', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'        => _x( '插入到'.$custom_post_type['post_type_Name'], 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item'   => _x( '上传至'.$custom_post_type['post_type_Name'], 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'       => _x( '筛选'.$custom_post_type['post_type_Name'].'列表', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation'   => _x( $custom_post_type['post_type_Name'].'列表导航', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'              => _x( $custom_post_type['post_type_Name'].'列表', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
        'item_published'          => $custom_post_type['post_type_Name']."已发布。",
        'item_published_privately'=>$custom_post_type['post_type_Name']."已私密发布。",
        'item_reverted_to_draft'  =>$custom_post_type['post_type_Name']."已恢复为草稿。",
        'item_scheduled'          =>$custom_post_type['post_type_Name']."已排入发布计划。",
        'item_updated'            =>$custom_post_type['post_type_Name']."已更新。",
        'item_link'               =>$custom_post_type['post_type_Name']."链接。",
        'item_link_description'   =>"目标".$custom_post_type['post_type_Name']."的链接。"
        
        
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => $custom_post_type['post_type_NameEn'] ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );
    register_post_type( $custom_post_type['post_type_NameEn'], $args );
}




foreach ($taxArray as $tax) {//新建自定义 tax 分类
    $labels = array(
        'name'              => _x( $tax['taxName'], 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( $tax['taxName'], 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( '搜素'.$tax['taxName'], 'textdomain' ),
        'all_items'         => __( '所有'.$tax['taxName'], 'textdomain' ),
        'view_item'         => __( '查看'.$tax['taxName'], 'textdomain' ),
        'parent_item'       => __( $tax['taxName'].'上级', 'textdomain' ),
        'parent_item_colon' => __( $tax['taxName'].'的上级:', 'textdomain' ),
        'edit_item'         => __( '修改'.$tax['taxName'], 'textdomain' ),
        'update_item'       => __( '更新'.$tax['taxName'], 'textdomain' ),
        'add_new_item'      => __( '添加新的'.$tax['taxName'], 'textdomain' ),
        'new_item_name'     => __( '新的'.$tax['taxName'].'名称', 'textdomain' ),
        'not_found'         => __( '没有找到相关的'.$tax['taxName'], 'textdomain' ),
        'back_to_items'     => __( '返回'.$tax['taxName'], 'textdomain' ),
        'menu_name'         => __( $tax['taxName'], 'textdomain' ),
    );
 
    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'public'            => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
        'show_in_rest'      => true,
    );
    register_taxonomy( $tax['taxNameEn'].'taxonomy', $tax['post_type'], $args );
}


}
 
add_action( 'init', 'custom_post_init' );

展示示例:

153b617ae249

如何调用

wordpress有自己的调用函数,你只需引入wp-load.php,就可以使用get_posts函数进行调用。比如我要调用我上面的文章类型一:

$args = array(
  'numberposts' => 10,
  'post_type'   => 'posttype1'
);
 
$posts = get_posts( $args );

比如我要调用上面文章类型一下面的分类一分类法下面的某个分类:

$taxposts = get_posts( array(
         'numberposts' => -1,
         'post_type'      => 'posttype1',//文章类型
         'taxSlu1taxonomy'          => $slugname,//分类法下新建的分类别名,也就是slug
         'post_status'    => 'publish',//文章状态为发布了的文章
) );

追加一个别人封装的

包括文章类型、标签、分类法

<?php
function news() {
    $labels = array(
        'name'                => _x( 'News', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'News', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'News', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent News:', 'text_domain' ),
        'all_items'           => __( 'All News', 'text_domain' ),
        'view_item'           => __( 'View News Info', 'text_domain' ),
        'add_new_item'        => __( 'Add New Article', 'text_domain' ),
        'add_new'             => __( 'Add News', 'text_domain' ),
        'edit_item'           => __( 'Edit News Info', 'text_domain' ),
        'update_item'         => __( 'Update News Info', 'text_domain' ),
        'search_items'        => __( 'Search News', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $rewrite = array(
        'slug'                => 'news',
        'with_front'          => true,
        'pages'               => true,
        'feeds'               => true,
    );
    $args = array(
        'label'               => __( 'News', 'text_domain' ),
        'description'         => __( 'Info News', 'text_domain' ),
        'labels'              => $labels,
        'show_in_rest'       => true, // To use Gutenberg editor.

        'supports'            => array( 'title', 'editor', 'thumbnail', 'comments'),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 7,
        'menu_icon'           => 'dashicons-admin-post',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'             => $rewrite,
    );
    register_post_type( 'news', $args );
}
add_action( 'init', 'news', 0 );
function news_tags_taxonomy() { $news_tags = array(
    'name' => _x( 'News Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'News Tag', 'taxonomy singular name' ),
    'search_items' => __( 'Search News Tags' ),
    'popular_items' => __( 'Popular News Tags' ),
    'all_items' => __( 'All News Tags' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit News Tag' ),
    'update_item' => __( 'Update News Tag' ),
    'add_new_item' => __( 'Add New News Tag' ),
    'new_item_name' => __( 'New News Tag Name' ),
    'separate_items_with_commas' => __( 'Separate News tags with commas' ),
    'add_or_remove_items' => __( 'Add or remove News tags' ),
    'choose_from_most_used' => __( 'Choose from the most used News tags' ),
    'menu_name' => __( 'News Tags' ), );
    register_taxonomy('news_tags','news', array(
        'hierarchical' => false,
        'labels' => $news_tags,
        'show_ui' => true,
        'show_in_rest'       => true,
        'show_admin_column' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'news_tags' ),
    ));
}
//add_action( 'init', 'news_tags_taxonomy', 0 );
function news_categories_taxonomy() {
    $label = array(
        'name' => _x( 'News Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'News Category',
            'taxonomy singular name' ), 'search_items' => __( 'Search news Categories' ),
        'all_items' => __( 'All News Categories' ),
        'parent_item' => __( 'Parent News Category' ),
        'parent_item_colon' => __( 'Parent News Category:' ),
        'edit_item' => __( 'Edit News Category' ),
        'update_item' => __( 'Update News Category' ),
        'add_new_item' => __( 'Add New News Category' ),
        'new_item_name' => __( 'New News Category' ),
        'menu_name' => __( 'News Categories' ),
    );
    register_taxonomy(
        'news_categories',array('news'), array(
        'hierarchical' => true,
        'labels' => $label,
        'show_in_rest'       => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'news_categories' ),
    ));
}
//add_action( 'init', 'news_categories_taxonomy', 0 );
function ex_themes_taxonomies_() {
    // Add genres

    $labels = array(
        'name'              => _x( 'Developer', 'taxonomy general name' ),
        'singular_name'     => _x( 'Developer', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Developer' ),
        'all_items'         => __( 'All Developer' ),
        'parent_item'       => __( 'Parent Developer' ),
        'parent_item_colon' => __( 'Parent Developer:' ),
        'edit_item'         => __( 'Edit Developer' ),
        'update_item'       => __( 'Update Developer' ),
        'add_new_item'      => __( 'Add New Developer' ),
        'new_item_name'     => __( 'New Developer Name' ),
        'menu_name'         => __( 'Developer' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'developer' ),
    );

    register_taxonomy( 'developer', array( 'post' ), $args );

}
add_action( 'init', 'ex_themes_taxonomies_', 0 );

版权说明

本文属于kekc原创,博客网址:https://www.kekc.cn 。转载请留版权,谢谢。

访问KEKC博客(www.kekc.cn),查看更多wordpress、PHP开发代码。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容