Solution for Creating a Portfolio Custom Post Type
Custom Code Solution
This code creates a Portfolio custom post type with relevant labels and support for titles, thumbnails, and excerpts. Place this code in your theme's functions.php
file or in a site-specific plugin:
function register_portfolio_post_type() {
$labels = array(
'name' => 'Portfolio',
'singular_name' => 'Portfolio Item',
'menu_name' => 'Portfolio',
'add_new' => 'Add New Item',
'add_new_item' => 'Add New Portfolio Item',
'edit_item' => 'Edit Portfolio Item',
'view_item' => 'View Portfolio Item',
'all_items' => 'All Portfolio Items'
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-portfolio', // WordPress built-in icon
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'rewrite' => array('slug' => 'portfolio'), // URL slug
'show_in_rest' => true, // Enable Gutenberg editor
);
register_post_type('portfolio', $args);
}
add_action('init', 'register_portfolio_post_type');
Add Portfolio Categories (Optional)
Add this code to create a custom taxonomy for portfolio categories:
function register_portfolio_taxonomy() {
$labels = array(
'name' => 'Portfolio Categories',
'singular_name' => 'Portfolio Category',
'search_items' => 'Search Categories',
'all_items' => 'All Categories',
'parent_item' => 'Parent Category',
'parent_item_colon' => 'Parent Category:',
'edit_item' => 'Edit Category',
'add_new_item' => 'Add New Category'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'portfolio-category'),
'show_in_rest' => true
);
register_taxonomy('portfolio_category', 'portfolio', $args);
}
add_action('init', 'register_portfolio_taxonomy');
Important Notes:
- After adding this code, visit Settings > Permalinks and save to flush rewrite rules
- Create template files in your theme:
archive-portfolio.php
for the portfolio archive and single-portfolio.php
for individual portfolio items
- The portfolio posts will be available at
yoursite.com/portfolio/
Plugin Alternatives
If you prefer using a plugin, here are reliable options:
-
Custom Post Type UI - Create custom post types through admin interface
-
Portfolio Post Type - Specifically designed for portfolio websites
-
Jetpack Portfolio - Part of Jetpack, includes portfolio features
Additional Customization
To add featured image support (if your theme doesn't already have it):
function add_portfolio_featured_image_support() {
add_theme_support('post-thumbnails');
add_image_size('portfolio-thumbnail', 300, 300, true);
}
add_action('after_setup_theme', 'add_portfolio_featured_image_support');
This solution provides a solid foundation for a portfolio section that you can further customize based on your specific needs.