5

给WordPress主题添加自定义文章类型register_post_type和分类教程

 2 years ago
source link: https://www.huhexian.com/25941.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

给WordPress主题添加自定义文章类型register_post_type和分类教程

青山 2022-02-1010:24:22评论2443字

wordpress作为一款简单实用的CMS系统,因为功能强大,便于开发,一直得到很多站长的青睐。最实用之处就是支持自定义文章类型和分类,并且非常的好用。本文章博主就来给大家简单的讲解一下如何在我们的主题中添加自定义文章类型register_post_type和分类register_taxonomy。

给WordPress主题添加自定义文章类型register_post_type和分类教程

1、添加自定义文章类型。

  1. /* Register Custom Post Type */
  2. add_action( 'init', 'create_products_post_type' );
  3. // add portfolio
  4. function create_products_post_type() {
  5. $labels = array(
  6. 'name' => __('产品', 'WPGP'),
  7. 'singular_name' => __('产品', 'WPGP'),
  8. 'add_new' => __('添加', 'WPGP'),
  9. 'add_new_item' => __('新增产品', 'WPGP'),
  10. 'edit_item' => __('编辑产品', 'WPGP'),
  11. 'new-item' => __('新增产品', 'WPGP'),
  12. 'view_item' => __('查看产品', 'WPGP'),
  13. 'search_items' => __('搜索产品', 'WPGP'),
  14. 'not_found' => __('未找到产品', 'WPGP'),
  15. 'not_found_in_trash' => __('垃圾箱未找到产品', 'WPGP'),
  16. 'parent_item_colon' => '',
  17. );
  18. $args = array(
  19. 'labels' => $labels,
  20. 'show_ui' => true, // Whether to generate a default UI for managing this post type in the admin
  21. 'query_var' => true,
  22. 'show_in_nav_menus' => false,
  23. 'public' => true, // Controls how the type is visible to authors and readers
  24. 'capability_type' => 'post',
  25. 'hierarchical' => false,
  26. 'menu_icon' => 'dashicons-format-gallery', // use a font icon, e.g. 'dashicons-chart-pie'
  27. 'has_archive' => true, // Enables post type archives
  28. 'rewrite' => array( 'slug' => 'products' ),
  29. 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'page-attributes' ),
  30. 'can_export' => true,
  31. );
  32. register_post_type( 'products', $args );
  33. }

2、添加分类功能

  1. add_action( 'init', 'register_products_taxonomy');
  2. // create two taxonomies, genres and writers for the post type "book"
  3. function register_products_taxonomy() {
  4. // Add new taxonomy, make it hierarchical (like categories)
  5. $labels = array(
  6. 'name' => __('产品分类', 'WPGP'),
  7. 'singular_name' => __('产品分类', 'WPGP'),
  8. 'menu_name' => __('产品分类', 'WPGP'),
  9. 'search_items' => __('搜索', 'WPGP'),
  10. 'all_items' => __('所有产品分类', 'WPGP'),
  11. 'parent_item' => __( '该产品分类的上级分类' ),
  12. 'parent_item_colon' => __( '该产品分类的上级分类:' ),
  13. 'edit_item' => __('编辑产品分类', 'WPGP'),
  14. 'update_item' => __('更新产品分类', 'WPGP'),
  15. 'add_new_item' => __('添加新的产品分类', 'WPGP'),
  16. 'new_item_name' => __('新的产品分类', 'WPGP'),
  17. $args = array(
  18. 'hierarchical' => true,
  19. 'labels' => $labels,
  20. 'show_ui' => true,
  21. 'show_in_menu' => true,
  22. 'show_in_nav_menus' => true,
  23. 'query_var' => true,
  24. 'has_archive' => false,
  25. 'show_admin_column' => true,
  26. 'rewrite' => array( 'slug' => 'product' ),
  27. register_taxonomy( 'product', 'products', $args );

3、添加后台自定义文章排序的功能

  1. // admin page products orderby
  2. add_filter( 'parse_query', 'sort_products_by_date' );
  3. function sort_products_by_date() {
  4. global $pagenow;
  5. if ( is_admin() && $pagenow =='edit.php' && !empty($_GET['post_type'] == 'products') && !isset($_GET['post_status']) && !isset($_GET['orderby']) ) {
  6. wp_redirect( admin_url('edit.php?post_type=products&orderby=date&order=desc') );
  7. exit;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK