- 資訊首頁(yè) > 互聯(lián)網(wǎng) > 經(jīng)驗分享 >
- 給WordPress主題添加自定義文章類(lèi)型register_post_ty
wordpress作為一款簡(jiǎn)單實(shí)用的CMS,因為功能強大,便于開(kāi)發(fā),一直得到很多站長(cháng)的青睞。最實(shí)用之處就是支持自定義文章類(lèi)型和分類(lèi),并且非常的好用。本文章博主就來(lái)給大家簡(jiǎn)單的講解一下如何在我們的主題中添加自定義文章類(lèi)型register_post_type和分類(lèi)register_taxonomy
/* Register Custom Post Type */
add_action( 'init', 'create_products_post_type' );
// add portfolio
function create_products_post_type() {
$labels = array(
'name' => __('產(chǎn)品', 'WPGP'),
'singular_name' => __('產(chǎn)品', 'WPGP'),
'add_new' => __('添加', 'WPGP'),
'add_new_item' => __('新增產(chǎn)品', 'WPGP'),
'edit_item' => __('編輯產(chǎn)品', 'WPGP'),
'new-item' => __('新增產(chǎn)品', 'WPGP'),
'view_item' => __('查看產(chǎn)品', 'WPGP'),
'search_items' => __('搜索產(chǎn)品', 'WPGP'),
'not_found' => __('未找到產(chǎn)品', 'WPGP'),
'not_found_in_trash' => __('垃圾箱未找到產(chǎn)品', '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 );
}
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' => __('產(chǎn)品分類(lèi)', 'WPGP'),
'singular_name' => __('產(chǎn)品分類(lèi)', 'WPGP'),
'menu_name' => __('產(chǎn)品分類(lèi)', 'WPGP'),
'search_items' => __('搜索', 'WPGP'),
'all_items' => __('所有產(chǎn)品分類(lèi)', 'WPGP'),
'parent_item' => __( '該產(chǎn)品分類(lèi)的上級分類(lèi)' ),
'parent_item_colon' => __( '該產(chǎn)品分類(lèi)的上級分類(lèi):' ),
'edit_item' => __('編輯產(chǎn)品分類(lèi)', 'WPGP'),
'update_item' => __('更新產(chǎn)品分類(lèi)', 'WPGP'),
'add_new_item' => __('添加新的產(chǎn)品分類(lèi)', 'WPGP'),
'new_item_name' => __('新的產(chǎn)品分類(lèi)', '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、添加后臺自定義文章排序的功能
// 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;
}
}
來(lái)源鏈接:https://www.zyhot.com/article/13218.html
本站聲明:網(wǎng)站內容來(lái)源于網(wǎng)絡(luò ),如有侵權,請聯(lián)系我們,我們將及時(shí)處理。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站