Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Custom Post Types

Custom Post Types

A introductory guide to WordPress Custom Post Types. Presented at WordCamp Sheffield 2014

Jenny Wong

April 26, 2014
Tweet

More Decks by Jenny Wong

Other Decks in Technology

Transcript

  1. Post  Types WordPress  deals  with  all  the  different  type  of

     content   by  de+ining  them  based  on  what  they  are.     ! The  de+initions  are  called  Post  Types.
  2. Custom  Post  Types • Post   • Page   •

    Attachments   • Revision   • Nav  Menus   •News   •Staff   …  anything  from  Jen’s  head
  3. Advantages •Hides  content  from  the  default  query   •Management  of

     content  is  easier   •Improved  user  interface  &  usability   •The  ability  to  use  custom  templates
  4. Disadvantages •Performance  issue  with  querying  a   large  table  

    •If  content  doesn’t  easily  +it  the  post   type  tables  then  issues  can  occur.  
  5. Theme •CPT  attached  to  the  theme   •Activate  Theme  &

     CPT  will  appear   •Deactivate  theme:   •  User  cannot  access  their  CPT  data   •  Data  still  stored  in  the  database
  6. Plugin •  Functionality  is  separate  to  the  design   of

     the  website   •Can  activate  new  theme  without   functionality  disappearing   •Plugins  can  be  deactivated
  7. Register  New  CPT <?php ! ! register_post_type( $post_type, $args );!

    ! $post_type! - Name of post type! $args! ! ! -! An array of arguments.! ! ! ! !
  8. Register  New  CPT <?php ! ! register_post_type( $post_type, $args );!

    ! $post_type! - Name of post type! $args! ! ! -! An array of arguments.! ! Full arguments list: ! https://codex.wordpress.org/Function_Reference/register_post_type#Arguments! ! !
  9. CPT  in  action <?php ! ! function cpt_wcsheff () {!

    ! ! $args = array( … );! ! ! register_post_type(‘speakers’, $args );! ! }! ! add_action( 'init','cpt_wcsheff');! !
  10. Categories  &  Tags function cpt_wcsheff () {! ! …! !

    register_post_type(‘speakers’, $args );! ! ! register_taxonomy_for_object_type( ! ! ! 'category', 'speakers' ! ! );! ! ! register_taxonomy_for_object_type( ! ! ! 'post_tag', 'speakers' ! ! );! }!
  11. Custom  Category $post_types = array(‘speakers’);! ! $category_args = array(! !

    'hierarchical' => true ! );! ! register_taxonomy( ! ! 'custom_cat', ! ! $post_types,! ! $category_args! );! !
  12. Custom  Tag $post_types = array(‘speakers’);! ! $category_args = array(! !

    'hierarchical' => false ! );! ! register_taxonomy( ! ! 'custom_tag', ! ! $post_types,! ! $category_args! );! !
  13. Custom  Templates ! archive-­‐$post_type.php   archive.php   ! single-­‐$post_type.php  

    single.php   ! taxonomy-­‐$taxonomy-­‐$term.php   taxonomy-­‐$taxonomy.php   taxonomy.php
  14. Permalinks URL  that  points  to  a  particular  location   !

    http://codex.wordpress.org/Using_Permalinks
  15. Flushing  in  a  plugin function cpt_wcsheff() {! register_post_type( ... );!

    }! ! ! function my_rewrite_flush() {! cpt_wcsheff();! flush_rewrite_rules();! }! ! ! register_activation_hook( ! ! __FILE__, ! ! 'my_rewrite_flush' ! );! ! !
  16. Flushing  in  a  theme function cpt_wcsheff() {! register_post_type( ... );!

    }! ! ! function cpt_wcsheff_rewrite_flush() {! flush_rewrite_rules();! }! ! add_action( ! ! 'after_switch_theme', ! 'cpt_wcsheff_rewrite_flush' ! );! ! !
  17. CPT  Naming Permalink  Structure:   ! $custom_post_type     =

     ‘wc_shef+ield’;   $custon_tax           =  ‘wc_shef+ield’;   ! example.com/$custom_post_type/post_slug   ! example.com/$custom_tax/another_slug