WordPress主题添加自定义文章类型register_post_type和分类法

WordPress主题添加自定义文章类型register_post_type和分类法,第1张

概述WordPress功能强大之处其中之一就是支持自定义文章类型和分类,非常的好用。本文给大家简单说一下如何在我们的主题中添加自定义文章类型register_post_type

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。

wordpress 功能强大之处其中之一就是支持自定义文章类型和分类,非常的好用。本文给大家简单说一下如何在我们的主题中添加自定义文章类型 register_post_type 和分类 register_taxonomy。首先,添加自定义文章类型:

/* Register Custom Post Type */

add_action( 'init','create_products_post_type' );

// add portfolio

function create_products_post_type() {

$labels = array(

'name' => __('产品','WPGP'),

'singular_name' => __('产品',

'add_new' => __('添加',

'add_new_item' => __('新增产品',

'edit_item' => __('编辑产品',

'new-item' => __('新增产品',

'vIEw_item' => __('查看产品',

'search_items' => __('搜索产品',

'not_found' => __('未找到产品',

'not_found_in_trash' => __('垃圾箱未找到产品',

'parent_item_colon' => '',

);

 

$args = array(

'labels' => $labels,

'show_ui' => true,// Whether to generate a default UI for managing this post type in the admin

'query_var' => true,

'show_in_nav_menus' => false,

'public' => true,// Controls how the type is visible to authors and readers

'capability_type' => 'post',

'hIErarchical' => false,

'menu_icon' => 'dashicons-format-gallery',// use a Font icon,e.g. 'dashicons-chart-pIE'

'has_archive' => true,// Enables post type archives

'rewrite' => array( 'slug' => 'products' ),

'supports' => array( 'Title','editor','thumbnail','excerpt','comments','custom-fIElds','page-attributes' ),

'can_export' => true,

);

 

register_post_type( 'products',$args );

}

接下来我们需要给自定义文章类型添加分类功能,来对自定义文章进行分类管理:

add_action( 'init','register_products_taxonomy');

 

// create two taxonomIEs,genres and writers for the post type "book"

function register_products_taxonomy() {

// Add new taxonomy,make it hIErarchical (like categorIEs)

$labels = array(

'name' => __('产品分类',

'singular_name' => __('产品分类',

'menu_name' => __('产品分类',

'search_items' => __('搜索',

'all_items' => __('所有产品分类',

'parent_item' => __( '该产品分类的上级分类' ),

'parent_item_colon' => __( '该产品分类的上级分类:' ),

'edit_item' => __('编辑产品分类',

'update_item' => __('更新产品分类',

'add_new_item' => __('添加新的产品分类',

'new_item_name' => __('新的产品分类',

);

 

$args = array(

'hIErarchical' => true,

'labels' => $labels,

'show_ui' => true,

'show_in_menu' => true,

'show_in_nav_menus' => true,

'query_var' => true,

'has_archive' => false,

'show_admin_column' => true,

'rewrite' => array( 'slug' => 'product' ),

);

 

register_taxonomy( 'product','products',$args );

}

最后,添加一个控制 wordpress 后台自定义文章页文章排序的小功能:

// admin page products orderby

add_filter( 'parse_query','sort_products_by_date' );

function sort_products_by_date() {

global $pageNow;

 

if ( is_admin() && $pageNow =='edit.PHP' && !empty($_GET['post_type'] == 'products') && !isset($_GET['post_status']) && !isset($_GET['orderby']) ) {

wp_redirect( admin_url('edit.PHP?post_type=products&orderby=date&order=desc') );

exit;

}

}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的WordPress主题添加自定义文章类型register_post_type和分类法全部内容,希望文章能够帮你解决WordPress主题添加自定义文章类型register_post_type和分类法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/1014486.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存