Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Custom Post Types
Search
Jenny Wong
April 26, 2014
Technology
160
0
Share
Custom Post Types
A introductory guide to WordPress Custom Post Types. Presented at WordCamp Sheffield 2014
Jenny Wong
April 26, 2014
More Decks by Jenny Wong
See All by Jenny Wong
The Road Taken
missjwo
0
240
Why and how to do a Site Health Check
missjwo
0
110
Getting ready for PHP7.2
missjwo
0
2.6k
is it possible to build a truly diverse community?
missjwo
0
420
Going Backstage on Community Trends: Bug or Feature?
missjwo
1
260
We Didn’t Care About Diversity — What Happened Next Is Amazing!
missjwo
0
280
Building on WordPress for Enterprise
missjwo
0
120
Building an Accessible Community - AlterConf Dublin Edition
missjwo
0
130
Building an Accessible Community
missjwo
2
250
Other Decks in Technology
See All in Technology
小さいVue.jsを30分で作る
hal_spidernight
0
150
CyberAgent YJC Connect
shimaf4979
1
170
freeeで運用しているAIQAについて
qatonchan
0
480
Digital Independence: Why, When and How
wannesrams
0
310
多角的な視点から見たAGI
terisuke
0
130
AI対話分析の夢と、汚いデータの現実 Looker / Dataplex / Dataform で実現する品質ファーストな基盤設計
waiwai2111
0
340
フロントエンドの相手が変わった - AIが加わったWebの新しいインターフェース設計
azukiazusa1
33
11k
クラウドネイティブ DB はいかにして制約を 克服したか? 〜進化歴史から紐解く、スケーラブルアーキテクチャ設計指針〜
hacomono
PRO
6
840
Purview 勉強会報告 Microsoft Purview 入門しようとしてみた
masakichixo
1
250
AIと乗り切った1,500ページ超のヘルプサイト基盤刷新とさらにその先の話
mugi_uno
2
330
サンプリングは「作る」のか「使う」のか? 分散トレースのコストと運用を両立する実践的戦略 / Why you need the tail sampling and why you don't want it
ymotongpoo
4
160
エージェント時代の UIとAPI、CLI戦略
coincheck_recruit
0
160
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.4k
How to Think Like a Performance Engineer
csswizardry
28
2.6k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.3k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
210
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
Scaling GitHub
holman
464
140k
エンジニアに許された特別な時間の終わり
watany
106
240k
Speed Design
sergeychernyshev
33
1.6k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
320
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
190
Transcript
Hello WC Shef+ield! Jenny Wong : Web Developer : @miss_jwo
Custom Post Types Jenny Wong : Web Developer : @miss_jwo
Fast Talker Feel free to ask me to slow down.
Its good for me.
Content The most important thing about a site.
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.
Post Types ! •Post •Page •Attachments •Revision
•Nav Menus } Defaults
Custom Post Types CPT for short.
Post Types ! •Post •Page •Attachments •Revision
•Nav Menus
Custom Post Types • Post • Page •
Attachments • Revision • Nav Menus •News •Staff … anything from Jen’s head
Why?
Advantages •Hides content from the default query •Management of
content is easier •Improved user interface & usability •The ability to use custom templates
Disadvantages •Performance issue with querying a large table
•If content doesn’t easily +it the post type tables then issues can occur.
Where do CPTs live? The Options
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
Plugin • Functionality is separate to the design of
the website •Can activate new theme without functionality disappearing •Plugins can be deactivated
Must Use Plugin mu plugin for short.
Must Use Plugin
Must Use Plugin https://codex.wordpress.org/Must_Use_Plugins
The Code
Register New CPT
Register New CPT <?php ! ! register_post_type( $post_type, $args );!
! $post_type! - Name of post type! $args! ! ! -! An array of arguments.! ! ! ! !
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! ! !
CPT in action <?php ! ! function cpt_wcsheff () {!
! ! $args = array( … );! ! ! register_post_type(‘speakers’, $args );! ! }! ! add_action( 'init','cpt_wcsheff');! !
Categories & Tags
Categories & Tags ! Default taxonomy: • category
• post_tag
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' ! ! );! }!
Custom Taxonomies
Custom Category $post_types = array(‘speakers’);! ! $category_args = array(! !
'hierarchical' => true ! );! ! register_taxonomy( ! ! 'custom_cat', ! ! $post_types,! ! $category_args! );! !
Custom Tag $post_types = array(‘speakers’);! ! $category_args = array(! !
'hierarchical' => false ! );! ! register_taxonomy( ! ! 'custom_tag', ! ! $post_types,! ! $category_args! );! !
Custom Taxonomies https://codex.wordpress.org/ Function_Reference/register_taxonomy
Templating
Custom Templates ! archive-‐$post_type.php archive.php ! single-‐$post_type.php
single.php ! taxonomy-‐$taxonomy-‐$term.php taxonomy-‐$taxonomy.php taxonomy.php
Condition Tags ! is_post_type_hierarchical( $post_type ); ! is_post_type_archive( $post_type
); ! post_type_exists( $post_type ) ; !
Common Issues
Permalinks URL that points to a particular location !
http://codex.wordpress.org/Using_Permalinks
Permalinks Issue: 404 error on a single CPT page
Flushing Permalinks ATTENTION: You should *NEVER EVER* do this on
every page load!!
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' ! );! ! !
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' ! );! ! !
Permalinks https://codex.wordpress.org/Function_Reference/ register_post_type#Flushing_Rewrite_on_Activation
Slugs
Slugs ! example.com/permalinks/slugs ! example.com/slugs !
CPT Naming
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
Questions?
Ask me later Jenny Wong : Web Developer : @miss_jwo