Skip to content

Instantly share code, notes, and snippets.

@dustyf
Created October 25, 2013 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dustyf/7157136 to your computer and use it in GitHub Desktop.
Save dustyf/7157136 to your computer and use it in GitHub Desktop.
The following code was created for the Milwaukee WordPress Meetup on 10/24/2013 demonstrating creating custom post types, custom taxonomies, and custom fields using Advanced Custom Fields.
/**
* Below is the code generated by GenerateWP for our custom post type and our
* custom field. We pasted this into our theme's functions.php file and it
* could also be added to a plugin file.
*/
// Register Custom Post Type
function wpmke_employees() {
$labels = array(
'name' => _x( 'Employees', 'Post Type General Name', 'wpmke_employees' ),
'singular_name' => _x( 'Employee', 'Post Type Singular Name', 'wpmke_employees' ),
'menu_name' => __( 'Employees', 'wpmke_employees' ),
'parent_item_colon' => __( 'Parent Employee', 'wpmke_employees' ),
'all_items' => __( 'All Employees', 'wpmke_employees' ),
'view_item' => __( 'View Employee', 'wpmke_employees' ),
'add_new_item' => __( 'Add New Employee', 'wpmke_employees' ),
'add_new' => __( 'New Employee', 'wpmke_employees' ),
'edit_item' => __( 'Edit Employee', 'wpmke_employees' ),
'update_item' => __( 'Update Employee', 'wpmke_employees' ),
'search_items' => __( 'Search Employees', 'wpmke_employees' ),
'not_found' => __( 'No employees found', 'wpmke_employees' ),
'not_found_in_trash' => __( 'No employees in the trash', 'wpmke_employees' ),
);
$rewrite = array(
'slug' => 'employees',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'wpmke_employee', 'wpmke_employees' ),
'description' => __( 'WordPress Milwaukee Employees', 'wpmke_employees' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', ),
'taxonomies' => array( 'wpmke_departments' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 25,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'wpmke_employee', $args );
}
// Hook into the 'init' action
add_action( 'init', 'wpmke_employees', 0 );
// Register Custom Taxonomy
function wpmke_departments() {
$labels = array(
'name' => _x( 'Departments', 'Taxonomy General Name', 'wpmke_departments' ),
'singular_name' => _x( 'Department', 'Taxonomy Singular Name', 'wpmke_departments' ),
'menu_name' => __( 'Department', 'wpmke_departments' ),
'all_items' => __( 'All Departments', 'wpmke_departments' ),
'parent_item' => __( 'Parent Department', 'wpmke_departments' ),
'parent_item_colon' => __( 'Parent Department:', 'wpmke_departments' ),
'new_item_name' => __( 'New Department', 'wpmke_departments' ),
'add_new_item' => __( 'Add New Department', 'wpmke_departments' ),
'edit_item' => __( 'Edit Department', 'wpmke_departments' ),
'update_item' => __( 'Update Department', 'wpmke_departments' ),
'separate_items_with_commas' => __( 'Separate department with commas', 'wpmke_departments' ),
'search_items' => __( 'Search Departments', 'wpmke_departments' ),
'add_or_remove_items' => __( 'Add or remove departments', 'wpmke_departments' ),
'choose_from_most_used' => __( 'Choose from most used departments', 'wpmke_departments' ),
);
$rewrite = array(
'slug' => 'departments',
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'rewrite' => $rewrite,
);
register_taxonomy( 'wpmke_departments', 'wpmke_employee', $args );
}
// Hook into the 'init' action
add_action( 'init', 'wpmke_departments', 0 );
/**
* Below is the page template file which we called single-wpmke_employee.php
* This will display the single page for our custom post type. We have
* added the custom fields created with Advanced Custom Fields to the template.
* I have cleaned up some of the extraneous code from the demo.
*/
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<!-- This is where the WordPress loop starts.
this displays our post. -->
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<!-- We are adding in our employee photo
above the title of the post (employee name) -->
<img src="<?php the_field('wpmke_photo'); ?>" />
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- We are adding in our job title
below the title of the post (employee name) -->
<h2><?php the_field('wpmke_job_title'); ?></h2>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->
<!-- We are going to add in more information just below
our content area. -->
<div>
<h2>Contact Information</h2>
<ul>
<!-- We are adding our phone and email
fields in a list -->
<li><?php the_field('wpmke_phone'); ?></li>
<li><?php the_field('wpmke_email'); ?></li>
</ul>
</div>
<div>
<h2>Certifications</h2>
<!-- Our certifications are in a repeater field. This
means it will have to loop through the repeater
to display each one -->
<?php if(get_field('wpmke_certifications')): // first we check if it exists ?>
<ul>
<?php while(has_sub_field('wpmke_certifications')): // then we do this stuff for each one ?>
<li><?php the_sub_field('wpmke_certification_name'); // display the certification name ?></li>
<?php endwhile; ?>
</ul>
<?php endif; // Be sure to end your if and while statements ?>
</div>
<h2>Start Date</h2>
<!-- We are using the date picker field to show the start date.
By default ACF returns a pretty ugly format for the date.
We will be converting this to a nicer format with PHP. You
can also edit the format in the ACF field, but doing it with
PHP gives you more control in using it in differne ways. -->
<?php $date = DateTime::createFromFormat('Ymd', get_field('start_date'));
echo $date->format('F n, Y'); ?>
<h2>Download PDF</h2>
<!-- We are also adding a file download to the employee page -->
<a href="<?php the_field('wpmke_pdf_bio'); ?>">Download</a>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment