Snippets

Benjamin J. DeLong WordPress Add Custom Post Type & shortcode (needs work)

Created by Benjamin J. DeLong
<?php
/*
    Author: bozdoz
    Author URI: https://twitter.com/bozdoz/
    Plugin Name: Rachael Simonds Media Posts
    Description: A plugin for creating Photography, Print, Drawing, and Other media types for Rachael Simonds.
    Version: 0.1.0
    License: GPL2
    */

add_action( 'init', 'add_all_custom_posts' );
function add_all_custom_posts() {

	$allmedia = array(
		array(
			'title' => 'Photography',
			'name' => 'photo'
		),
		array(
			'title' => 'Print',
			'name' => 'print'
		),
		array(
			'title' => 'Drawing',
			'name' => 'drawing'
		),
		array(
			'title' => 'Other Media',
			'name' => 'other'
		),
		array(
			'title' => 'Photography',
			'name' => 'photo'
		),
	);

	function getArgs ($arr) {
		
		extract($arr);

		$labels = array(
			'name'               => _x( "$title", 'post type general name' ),
			'singular_name'      => _x( "$name", 'post type singular name' ),
			'add_new'            => _x( "Add New", "$name" ),
			'add_new_item'       => __( "Add New $name" ),
			'edit_item'          => __( "Edit $name" ),
			'new_item'           => __( "New $name" ),
			'all_items'          => __( "All $title" ),
			'view_item'          => __( "View $name" ),
			'search_items'       => __( "Search $title" ),
			'not_found'          => __( "No $title found" ),
			'not_found_in_trash' => __( "No $title found in the Trash" ), 
			'parent_item_colon'  => '',
			'menu_name'          => "$title"
		);
		$args = array(
			'labels'        => $labels,
			'description'   => "Holds our $title and $name specific data",
			'public'        => true,
			'menu_position' => 5,
			'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
			'has_archive'   => true,
		);
		return $args;
	}

	foreach ($allmedia as $arr) {
		register_post_type($arr['name'], getArgs($arr) );
	}
}

/* shortcode for displaying all custom media posts */
add_shortcode('rachael-simonds-media', 'displayMedia');
function displayMedia () {
    $args = array(
    	'posts_per_page'   => 40,
    	'orderby'          => 'id',
    	'order'            => 'DESC',
    	'post_type'        => 'team',
    	'post_status'      => 'publish'); 
    
    $teams = get_posts( $args );
    $html = '<div class="team-list">';
    foreach ( $teams as $team ) {
    	$thumb_url = get_the_post_thumbnail_url($team->ID, array(400,400)); 
    	$permalink = get_permalink($team->ID); 
    	$position = get_post_meta($team->ID, 'team_position', true);

    	$html .= "<a class=\"img-holder\" 
			href=\"$permalink\"
			title=\"$team->post_title\">
			<span class=\"img img-block\"
				style=\"background-image:url( $thumb_url );\">
				<span class=\"img-screen\">
					<span class=\"center-vertical\">
					<span class=\"img-title\">$team->post_title</span>
					<span class=\"img-subtitle\">$position</span>
					</span>
				</span>
			</span>
		</a>";
    }
    $html .= '</div>';
    wp_reset_postdata();
    return $html;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.