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

1、添加自定义文章类型。
/* Register Custom Post Type */ 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、添加后台自定义文章排序的功能
// 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;
}
}
本站除原创内容,其余内容均收集自互联网,仅限用于学习和研究目的,本站不对其内容的合法性、可用性承担任何责任。本站禁止以任何形式发布或转载任何违法相关信息,若您发现请立即向站长举报;用户投稿一切资源内容不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
如有版权内容,其版权均归原作者所有,本站虽力求保存原有版权信息,但因众多资源经多次转载,已无法确定其真实来源,故敬请原作者谅解!为尊重作者劳动成果,请购买正版支持作者,谢谢!若您对「HULIKU.COM」所载资源作品版权归属存有异议,请发送邮箱:ihuliku@qq.com 进行处理。
本站资源文件大多存储至云盘,如您发现图片或链接失效,请联系站长或作者,我们将及时进行更新。资源都是经过站长或作者收集测试优化后进行发布分享。如若转载请在贵站文内以超链形式注明狐狸库文章出处,谢谢合作!
请登录后查看评论内容