欢迎访问服务器技术网-www.fuwuqijishu.com

WordPress添加自定义文章类型/分类功能和自定义排序

网站建设 fuwuqijishu 2年前 (2022-09-04) 25次浏览 0个评论 扫描二维码

我们在选择WordPress程序的时候,就是考虑到可以扩展的功能是比较多的。我们可选择的主题、插件基本上可以实现需要的功能。比如我们在用WordPress程序做企业网站的时候,可能需要自定义文章类型和分类功能,在这里整理出来Wordpress实现自定义文章类型,分类以及排序。

1、添加自定义文章类型

/* 添加自定义文章类型 itbulu.com *
/add_actiON( ‘init’, ‘create_products_post_type’ );
// add portfolio
function create_products_post_type() {
$labels = array(
‘name’ => __(‘产品’, ‘WPGP’),
‘singular_name’ => __(‘产品’, ‘WPGP’),
‘add_new’ => __(‘添加’, ‘WPGP’),
‘add_new_item’ => __(‘新增产品’, ‘WPGP’),
‘edit_item’ => __(‘编辑产品’, ‘WPGP’),
‘new-item’ => __(‘新增产品’, ‘WPGP’),
‘view_item’ => __(‘查看产品’, ‘WPGP’),
‘search_items’ => __(‘搜索产品’, ‘WPGP’),
‘not_found’ => __(‘未找到产品’, ‘WPGP’),
‘not_found_in_trash’ => __(‘垃圾箱未找到产品’, ‘WPGP’),
‘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 );
}

2、添加分类功能

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’ => __(‘产品分类’, ‘WPGP’),
‘singular_name’ => __(‘产品分类’, ‘WPGP’),
‘menu_name’ => __(‘产品分类’, ‘WPGP’),
‘search_items’ => __(‘搜索’, ‘WPGP’),
‘all_items’ => __(‘所有产品分类’, ‘WPGP’),
‘parent_item’ => __( ‘该产品分类的上级分类’ ),
‘parent_item_colon’ => __( ‘该产品分类的上级分类:’ ),
‘edit_item’ => __(‘编辑产品分类’, ‘WPGP’),
‘update_item’ => __(‘更新产品分类’, ‘WPGP’),
‘add_new_item’ => __(‘添加新的产品分类’, ‘WPGP’),
‘new_item_name’ => __(‘新的产品分类’, ‘WPGP’),
);

$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 );
}

3、自定义文章排序

// 文章排序 itbulu.com
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;
}
}

我们可以根据实际需要再调整。

喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Warning: error_log(/www/wwwroot/fuwiqijishu/wp-content/plugins/spider-analyser/#log/log-2208.txt): failed to open stream: No such file or directory in /www/wwwroot/fuwiqijishu/wp-content/plugins/spider-analyser/spider.class.php on line 2900