🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
无论是直接使用插件,还是参考代码,Custom Post Type UI都很有参考作用,下载链接: https://wordpress.org/plugins/custom-post-type-ui/ 在functions.php上加入以下代码: ~~~ // custom post type function awesome_custom_post_type(){ $labels = array( 'name' => 'Porfolio', 'singular_name' =>'Portfolio', 'add_new' => 'Add Item', 'all_items' => 'All Items', 'add_new_item' => 'Edit Item', 'new_item' => 'New Item', 'view_item' => 'View Items', 'search_item' => 'Search Portfolio', 'not_found' => 'No items fouond', 'not_found_in_trash' => 'No items found in trash', 'parent_item_colon' => 'Parent Item' ); $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'publicly_queryable' => true, 'menu_icon' =>'dashicons-welcome-write-blog', 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'support' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', ), 'taxonomies' => array('category', 'post_tag'), 'menu_position' => 5, 'exclude_from_search' => false ); register_post_type('portfolio', $args); } add_action('init','awesome_custom_post_type'); ~~~ admin界面会多出和文章一样格式的Porfolio(作品): ![](https://box.kancloud.cn/53e76d3218c9bc039fb7bf57b1f4509b_423x237.png) 创建一个专页来显示作品集: 创建page-portfolio.php,加上以下代码: ~~~ <?php /* * Template Name: Portfolio Template */ ?> <?php get_header(); ?> <?php $args=array('post_type' => 'portfolio', 'post_per_page' => 3); $loop=new WP_Query($args); if($loop->have_post()): while(have_posts()): the_post(); ?> <?php get_template_part('content', 'archive'); ?> <?php endwhile; endif; ?> <?php get_footer(); ?> ~~~ 在创建的页面上,选择Portfolio Template作为页面模板。