Categories
WordPress

Creating A Portfolio Website in WordPress Using Custom Post Types

Presentation begins today at 3 PM PT

Download sample plugins referenced during the presentation.

<?php
/**
 * Plugin Name: Portfolio Custom Post Type
 * Description: A custom post type for an additional portfolio blog
 * Author: Joseph Dickson
 * Author URI: https://joseph-dickson.com
 * Version: 1.0
 * License: GPL2
 */

// Prevent direct access to this file
if ( ! defined( 'ABSPATH' ) ) exit;

// defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

/**
 * Create a custom post type  
 * https://codex.wordpress.org/Function_Reference/register_post_type#Arguments
 */
function jd_portfolio_custom_post_type() {

$labels = array(
	'name'			=> 'Portfolio',
	'singular_name'		=> 'Portfolio',
	'menu_name'		=> 'Portfolio',
	'name_admin_bar'	=> 'Portfolio',
	'add_new'		=> 'Add a Portfolio Post',
	'add_new_item' 		=> 'Portfolio Post',
	'new_item'		=> 'Portfolio Post',
	'edit_item'		=> 'Edit Portfolio Post',
	'view_item'		=> 'View Portfolio Post',
	'all_items'		=> 'All Portfolio Posts',
	'search_items' 		=> 'Search Portfolio Posts',
	'parent_item_colon'	=> 'Parent Portfolio Posts:',
	'not_found'		=> 'No Portfolio Posts found.',
	'not_found_in_trash' 	=> 'No Portfolio Posts found in Trash.',
);

$args = array(
	'labels'	     	=> $labels,
	'public'		=> true,
	'publicly_queryable'	=> true,
	'show_ui'		=> true,
	'show_in_menu'		=> true,
	'menu_icon'		=> 'dashicons-format-gallery',
	'query_var'		=> true,
	'rewrite'		=> array( 'slug' => 'portfolio' ),
	'capability_type'	=> 'post',
	'has_archive'		=> true,
	'hierarchical'		=> false,
	'menu_position'		=> 21,
	'show_in_rest'		=> true,
	'supports'		=> array( 'title', 'editor', 'thumbnail' ),
);

register_post_type( 'portfolio', $args );
}

add_action( 'init', 'jd_portfolio_custom_post_type' );

// Flush rewrite rules when plugin is activated
function jd_portfolio_rewrite_flush() {
	jd_portfolio_custom_post_type();
	flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'jd_rewrite_flush' );
<?php
/**
 * Plugin Name: Services Custom Post Type
 * Description: A custom post type Services Pages 
 * Version: 1.0
 * License: GPL2
 */

// Prevent direct access to this file
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Create a custom post type  
 * https://codex.wordpress.org/Function_Reference/register_post_type#Arguments
 */
function jd_services_custom_post_type() {

$labels = array(
	'name'			=> 'Services',
	'singular_name'		=> 'Services',
	'menu_name'		=> 'Services',
	'name_admin_bar'	=> 'Services',
	'add_new'		=> 'Add a Services Page',
	'add_new_item'		=> 'Services Page',
	'new_item'		=> 'Services Page',
	'edit_item'		=> 'Edit Services Page',
	'view_item'		=> 'View Services Page',
	'all_items'		=> 'All Services Pages',
	'search_items'		=> 'Search Services Pages',
	'parent_item_colon'	=> 'Parent Services Pages:',
	'not_found'		=> 'No Services Pages found.',
	'not_found_in_trash'	=> 'No Services Pages found in Trash.',
);

$args = array(
	'labels'		=> $labels,
	'public'		=> true,
	'publicly_queryable'	=> true,
	'show_ui'		=> true,
	'show_in_menu'		=> true,
	'menu_icon'		=> 'dashicons-media-document',
	'query_var'		=> true,
	'rewrite'		=> array( 'slug' => 'services' ),
	'capability_type'	=> 'page',
	'has_archive'		=> false,
	'hierarchical'		=> true,
	'menu_position'		=> 22,
	'show_in_rest'		=> true,
	'supports'		=> array( 'title', 'editor', 'thumbnail' ),
);

register_post_type( 'services', $args );
}

add_action( 'init', 'jd_services_custom_post_type' );

// Flush rewrite rules when plugin is activated
function jd_services_rewrite_flush() {
	jd_services_custom_post_type();
	flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'jd_rewrite_flush' );

By Joseph Dickson

Joseph Dickson is a web developer in higher education and an avid proponent of using WordPress’ core features to create flexible and manageable design solutions.