国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

給WordPress主題添加自定義文章類(lèi)型register_post_ty

發(fā)布時(shí)間:2022-06-23 10:31 來(lái)源:資源部落 閱讀:143 作者:資源部落 欄目: 經(jīng)驗分享 歡迎投稿:712375056

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

1、添加自定義文章類(lèi)型

/* 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 );
}

2、添加分類(lèi)功能

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í)歡迎投稿傳遞力量。

人妻系列无码中文字幕专区| 久久夜色撩人精品国产| 狠狠躁夜夜躁人人爽天天古典| 国产亚洲欧美精品一区| 无码精品A∨在线观看十八禁| 欧美 日本 亚洲 视频|