SlideShare uma empresa Scribd logo
1 de 15
Custom Layouts Without
            Using Page Templates
                                    Chip Bennett (@chip_bennett)




WordCamp Nashville 2013, 20 April 2013         Page 1 of 15   Custom Layouts Without Using Page Templates
Overview
        •      What's the problem?
                 o     Custom Content, Custom Layouts
                 o     How to Have Both
        •      Process
                 o     Define Post Custom Meta Data
                 o     Modify Template Files
                 o     Define CSS
        •      Putting it into Practice
                 o     Twenty Twelve Child Theme
        •      Out of Presentation Scope
                 o     Post Custom Meta Implementation
WordCamp Nashville 2013, 20 April 2013    Page 2 of 15   Custom Layouts Without Using Page Templates
What's the Problem?
        •      Purpose of Custom Page Templates
                o    Custom content!
                o    Archives
                o    Linkroll
                o    Sitemap
                o    Contact form
                o    Custom queries

        •      Most Common use of Custom Page Templates
                o    Layouts
                o    Full Width
                o    Different Sidebars
                o    Etc.
WordCamp Nashville 2013, 20 April 2013    Page 3 of 15   Custom Layouts Without Using Page Templates
What's the Problem?
        •     Custom Page Templates for both custom content and custom
              layout?
               o    More templates?
               o    Sitemap,
               o    Sitemap Full-Width,
               o    Sitemap Three-Column
               o    Archives,
               o    Archives Full-Width,
               o    Archives Three-Column
               o    Linkroll,
               o    Linkroll Full-Width,
               o    Linkroll Three-Column

        •     See where this is going?
WordCamp Nashville 2013, 20 April 2013      Page 4 of 15   Custom Layouts Without Using Page Templates
Alternative: Custom Post Meta Data
        •      Benefits
                o     Per-page
                o     Regardless of Page Template
                o     Page template itself is custom post meta data
                o     _wp_page_template

        •      Extra Benefits?
                o     Can be made to work with (almost) any Theme
                o     Via Child Theme, Some coding/CSS required
                o     Expand to other post-types
                o     Blog Posts, Attachments, Custom Post Types
                o     Plugin-defined page templates
                o     Expand/Dovetail with default layout options
WordCamp Nashville 2013, 20 April 2013        Page 5 of 15          Custom Layouts Without Using Page Templates
Implementation
        •       Define Layout Custom Post Meta Data
                 o      Default layout is two-column
                 o      Add a "Full Width" layout option
        •       Modify Template According to Layout
                 o      Full Width layout removes the sidebar
                 o      Content takes up resultant space
        •       Modify CSS According to Layout
                 o      Modify main-content width CSS rule



WordCamp Nashville 2013, 20 April 2013      Page 6 of 15        Custom Layouts Without Using Page Templates
Define Layout Custom Post Meta
        •       Recommended Nomenclature:
                _themename_layout
                 o      Underscore: hide from custom fields UI
                 o      Namespacing: avoid conflicts, per-Theme
        •       Reminder:
                 o      Custom Post Meta code is out of presentation scope
                 o      Working example will be provided




WordCamp Nashville 2013, 20 April 2013     Page 7 of 15     Custom Layouts Without Using Page Templates
Custom Function to Get Layout
        •       Define a function to get the current layout
                        function themename_get_layout() {
                           $layout = 'default';
                           global $post;
                           $post_layout = get_post_meta( $post->ID,
                        '_themename_layout', true );
                           if ( '' != $post_layout ) {
                               $layout = $post_layout;
                           }
                            return $layout;
                        }

        •       We'll use this in a couple places, so DRY


WordCamp Nashville 2013, 20 April 2013      Page 8 of 15     Custom Layouts Without Using Page Templates
Modify the Template
        •       Typical template file:
                        <?php
                        // Header
                        get_header();
                        // Loop
                        get_template_part( 'loop' );
                        // Sidebar
                        get_sidebar();
                        // Footer
                        get_footer();
                        ?>

        •       index.php, archive.php, etc.
        •       Child Theme: let's just modify sidebar.php
WordCamp Nashville 2013, 20 April 2013      Page 9 of 15   Custom Layouts Without Using Page Templates
Modify the Template
        •       Modifying sidebar.php
                 o      Before:
                                 <?php
                                 // Sidebar HTML Markup & PHP code
                                 ?>
                 o      After:
                                 <?php
                                 if ( 'full' == themename_get_layout() ) {
                                    return;
                                 }
                                 // Sidebar HTML Markup & PHP code
                                 ?>

        •       Adapt as needed
WordCamp Nashville 2013, 20 April 2013           Page 10 of 15       Custom Layouts Without Using Page Templates
Modify CSS
        •       Add Layout-Based Classes to <body> tag
                 o      Use body_class filter:
                        function themename_filter_body_class( $classes ) {
                           $classes[] = 'layout-' . themename_get_layout();
                           return $classes;
                        }
                        add_filter( 'body_class',
                        'themename_filter_body_class' );

        •       Result:
                        <body class="page template-default layout-full ...">

        •       Child Theme: add layout-based CSS rules:
                        body.layout-full #content {
                           width: 100%;
                        }
WordCamp Nashville 2013, 20 April 2013      Page 11 of 15      Custom Layouts Without Using Page Templates
Practical Exercise: Twenty Twelve
        •       Let's see an example
        •       Add custom layouts to Twenty Twelve
        •       Via Child Theme
        •       Child Theme Files
                 o      style.css
                 o      functions.php
                 o      sidebar.php




WordCamp Nashville 2013, 20 April 2013   Page 12 of 15   Custom Layouts Without Using Page Templates
Twenty Twelve Live
            Example
                      (Child Theme is available for download)




WordCamp Nashville 2013, 20 April 2013   Page 13 of 15   Custom Layouts Without Using Page Templates
Links and Resources
        •      Downloads
                 o     WCNash13 TwentyTwelve Child Theme
                 o     http://github.com/chipbennett/wcnash13-twentytwelve-child
                 o     Oenology Theme (advanced layout example)
                 o     http://github.com/chipbennett/oenology

        •      Codex References
                 o     http://codex.wordpress.org/Pages#Page_Templates
                 o     http://codex.wordpress.org/Function_Reference/get_post_meta
                 o     http://codex.wordpress.org/Function_Reference/update_post_meta
                 o     http://codex.wordpress.org/Function_Reference/add_meta_box
                 o     http://codex.wordpress.org/Plugin_API/Filter_Reference/body_class

        •      Presentation Slides
                 o     http://www.slideshare.net/chipbennett
WordCamp Nashville 2013, 20 April 2013              Page 14 of 15           Custom Layouts Without Using Page Templates
Feedback
        •       Questions or comments?




WordCamp Nashville 2013, 20 April 2013   Page 15 of 15   Custom Layouts Without Using Page Templates

Mais conteúdo relacionado

Semelhante a WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates

WordCamp Columbus 2013: Custom Layouts Without Using Page Templates
WordCamp Columbus 2013: Custom Layouts Without Using Page TemplatesWordCamp Columbus 2013: Custom Layouts Without Using Page Templates
WordCamp Columbus 2013: Custom Layouts Without Using Page TemplatesChip Bennett
 
How To Choose A Theme
How To Choose A ThemeHow To Choose A Theme
How To Choose A ThemeNicky Pink
 
Drupaldelphia Shortcuts Cheats And Cheap Stunts
Drupaldelphia  Shortcuts Cheats And Cheap StuntsDrupaldelphia  Shortcuts Cheats And Cheap Stunts
Drupaldelphia Shortcuts Cheats And Cheap Stuntscanarymason
 
Drupal 8 theming deep dive
Drupal 8 theming deep diveDrupal 8 theming deep dive
Drupal 8 theming deep diveRomain Jarraud
 
WordPress Theming Best Practices
WordPress Theming Best PracticesWordPress Theming Best Practices
WordPress Theming Best PracticesBrian Krogsgard
 
Drupal 7 Search Engine Optimisation
Drupal 7 Search Engine OptimisationDrupal 7 Search Engine Optimisation
Drupal 7 Search Engine OptimisationPeter Macinkovic
 
Writing an extensible web testing framework ready for the cloud slide share
Writing an extensible web testing framework ready for the cloud   slide shareWriting an extensible web testing framework ready for the cloud   slide share
Writing an extensible web testing framework ready for the cloud slide shareMike Ensor
 
Getting Started With WordPress Themes for Beginners
Getting Started With WordPress Themes for BeginnersGetting Started With WordPress Themes for Beginners
Getting Started With WordPress Themes for BeginnersNew Tricks
 
Theme frameworks & child themes
Theme frameworks & child themesTheme frameworks & child themes
Theme frameworks & child themesChris Olbekson
 
New Adventures in Drupal Theming
New Adventures in Drupal ThemingNew Adventures in Drupal Theming
New Adventures in Drupal ThemingJohn Albin Wilkins
 
Child Theme Frameworks
Child Theme FrameworksChild Theme Frameworks
Child Theme Frameworksryngrn
 
Getting started with CSS frameworks using Zurb foundation
Getting started with CSS frameworks using Zurb foundationGetting started with CSS frameworks using Zurb foundation
Getting started with CSS frameworks using Zurb foundationMelanie Archer
 
Keeping Your Themes and Plugins Organized.
Keeping Your Themes and Plugins Organized.Keeping Your Themes and Plugins Organized.
Keeping Your Themes and Plugins Organized.Jacob Martella
 
WordPress: After The Install
WordPress: After The InstallWordPress: After The Install
WordPress: After The InstallWordPress NYC
 
Developing Custom WordPress Themes for Clients
Developing Custom WordPress Themes for ClientsDeveloping Custom WordPress Themes for Clients
Developing Custom WordPress Themes for ClientsSteven Slack
 
Drupal8 themingdeepdive drupaldevdays-montpellier17042015
Drupal8 themingdeepdive drupaldevdays-montpellier17042015Drupal8 themingdeepdive drupaldevdays-montpellier17042015
Drupal8 themingdeepdive drupaldevdays-montpellier17042015Romain Jarraud
 

Semelhante a WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates (20)

WordCamp Columbus 2013: Custom Layouts Without Using Page Templates
WordCamp Columbus 2013: Custom Layouts Without Using Page TemplatesWordCamp Columbus 2013: Custom Layouts Without Using Page Templates
WordCamp Columbus 2013: Custom Layouts Without Using Page Templates
 
Efficient theming in Drupal
Efficient theming in DrupalEfficient theming in Drupal
Efficient theming in Drupal
 
How To Choose A Theme
How To Choose A ThemeHow To Choose A Theme
How To Choose A Theme
 
Drupaldelphia Shortcuts Cheats And Cheap Stunts
Drupaldelphia  Shortcuts Cheats And Cheap StuntsDrupaldelphia  Shortcuts Cheats And Cheap Stunts
Drupaldelphia Shortcuts Cheats And Cheap Stunts
 
Drupal 8 theming deep dive
Drupal 8 theming deep diveDrupal 8 theming deep dive
Drupal 8 theming deep dive
 
WordPress Theming Best Practices
WordPress Theming Best PracticesWordPress Theming Best Practices
WordPress Theming Best Practices
 
Drupal 7 Search Engine Optimisation
Drupal 7 Search Engine OptimisationDrupal 7 Search Engine Optimisation
Drupal 7 Search Engine Optimisation
 
Writing an extensible web testing framework ready for the cloud slide share
Writing an extensible web testing framework ready for the cloud   slide shareWriting an extensible web testing framework ready for the cloud   slide share
Writing an extensible web testing framework ready for the cloud slide share
 
Getting Started With WordPress Themes for Beginners
Getting Started With WordPress Themes for BeginnersGetting Started With WordPress Themes for Beginners
Getting Started With WordPress Themes for Beginners
 
Nanocon Taiwan
Nanocon TaiwanNanocon Taiwan
Nanocon Taiwan
 
Theme frameworks & child themes
Theme frameworks & child themesTheme frameworks & child themes
Theme frameworks & child themes
 
New Adventures in Drupal Theming
New Adventures in Drupal ThemingNew Adventures in Drupal Theming
New Adventures in Drupal Theming
 
Forensic Theming for Drupal
Forensic Theming for DrupalForensic Theming for Drupal
Forensic Theming for Drupal
 
Child Theme Frameworks
Child Theme FrameworksChild Theme Frameworks
Child Theme Frameworks
 
Getting started with CSS frameworks using Zurb foundation
Getting started with CSS frameworks using Zurb foundationGetting started with CSS frameworks using Zurb foundation
Getting started with CSS frameworks using Zurb foundation
 
Forensic Theming - DrupalCon London
Forensic Theming - DrupalCon LondonForensic Theming - DrupalCon London
Forensic Theming - DrupalCon London
 
Keeping Your Themes and Plugins Organized.
Keeping Your Themes and Plugins Organized.Keeping Your Themes and Plugins Organized.
Keeping Your Themes and Plugins Organized.
 
WordPress: After The Install
WordPress: After The InstallWordPress: After The Install
WordPress: After The Install
 
Developing Custom WordPress Themes for Clients
Developing Custom WordPress Themes for ClientsDeveloping Custom WordPress Themes for Clients
Developing Custom WordPress Themes for Clients
 
Drupal8 themingdeepdive drupaldevdays-montpellier17042015
Drupal8 themingdeepdive drupaldevdays-montpellier17042015Drupal8 themingdeepdive drupaldevdays-montpellier17042015
Drupal8 themingdeepdive drupaldevdays-montpellier17042015
 

Último

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates

  • 1. Custom Layouts Without Using Page Templates Chip Bennett (@chip_bennett) WordCamp Nashville 2013, 20 April 2013 Page 1 of 15 Custom Layouts Without Using Page Templates
  • 2. Overview • What's the problem? o Custom Content, Custom Layouts o How to Have Both • Process o Define Post Custom Meta Data o Modify Template Files o Define CSS • Putting it into Practice o Twenty Twelve Child Theme • Out of Presentation Scope o Post Custom Meta Implementation WordCamp Nashville 2013, 20 April 2013 Page 2 of 15 Custom Layouts Without Using Page Templates
  • 3. What's the Problem? • Purpose of Custom Page Templates o Custom content! o Archives o Linkroll o Sitemap o Contact form o Custom queries • Most Common use of Custom Page Templates o Layouts o Full Width o Different Sidebars o Etc. WordCamp Nashville 2013, 20 April 2013 Page 3 of 15 Custom Layouts Without Using Page Templates
  • 4. What's the Problem? • Custom Page Templates for both custom content and custom layout? o More templates? o Sitemap, o Sitemap Full-Width, o Sitemap Three-Column o Archives, o Archives Full-Width, o Archives Three-Column o Linkroll, o Linkroll Full-Width, o Linkroll Three-Column • See where this is going? WordCamp Nashville 2013, 20 April 2013 Page 4 of 15 Custom Layouts Without Using Page Templates
  • 5. Alternative: Custom Post Meta Data • Benefits o Per-page o Regardless of Page Template o Page template itself is custom post meta data o _wp_page_template • Extra Benefits? o Can be made to work with (almost) any Theme o Via Child Theme, Some coding/CSS required o Expand to other post-types o Blog Posts, Attachments, Custom Post Types o Plugin-defined page templates o Expand/Dovetail with default layout options WordCamp Nashville 2013, 20 April 2013 Page 5 of 15 Custom Layouts Without Using Page Templates
  • 6. Implementation • Define Layout Custom Post Meta Data o Default layout is two-column o Add a "Full Width" layout option • Modify Template According to Layout o Full Width layout removes the sidebar o Content takes up resultant space • Modify CSS According to Layout o Modify main-content width CSS rule WordCamp Nashville 2013, 20 April 2013 Page 6 of 15 Custom Layouts Without Using Page Templates
  • 7. Define Layout Custom Post Meta • Recommended Nomenclature: _themename_layout o Underscore: hide from custom fields UI o Namespacing: avoid conflicts, per-Theme • Reminder: o Custom Post Meta code is out of presentation scope o Working example will be provided WordCamp Nashville 2013, 20 April 2013 Page 7 of 15 Custom Layouts Without Using Page Templates
  • 8. Custom Function to Get Layout • Define a function to get the current layout function themename_get_layout() { $layout = 'default'; global $post; $post_layout = get_post_meta( $post->ID, '_themename_layout', true ); if ( '' != $post_layout ) { $layout = $post_layout; } return $layout; } • We'll use this in a couple places, so DRY WordCamp Nashville 2013, 20 April 2013 Page 8 of 15 Custom Layouts Without Using Page Templates
  • 9. Modify the Template • Typical template file: <?php // Header get_header(); // Loop get_template_part( 'loop' ); // Sidebar get_sidebar(); // Footer get_footer(); ?> • index.php, archive.php, etc. • Child Theme: let's just modify sidebar.php WordCamp Nashville 2013, 20 April 2013 Page 9 of 15 Custom Layouts Without Using Page Templates
  • 10. Modify the Template • Modifying sidebar.php o Before: <?php // Sidebar HTML Markup & PHP code ?> o After: <?php if ( 'full' == themename_get_layout() ) { return; } // Sidebar HTML Markup & PHP code ?> • Adapt as needed WordCamp Nashville 2013, 20 April 2013 Page 10 of 15 Custom Layouts Without Using Page Templates
  • 11. Modify CSS • Add Layout-Based Classes to <body> tag o Use body_class filter: function themename_filter_body_class( $classes ) { $classes[] = 'layout-' . themename_get_layout(); return $classes; } add_filter( 'body_class', 'themename_filter_body_class' ); • Result: <body class="page template-default layout-full ..."> • Child Theme: add layout-based CSS rules: body.layout-full #content { width: 100%; } WordCamp Nashville 2013, 20 April 2013 Page 11 of 15 Custom Layouts Without Using Page Templates
  • 12. Practical Exercise: Twenty Twelve • Let's see an example • Add custom layouts to Twenty Twelve • Via Child Theme • Child Theme Files o style.css o functions.php o sidebar.php WordCamp Nashville 2013, 20 April 2013 Page 12 of 15 Custom Layouts Without Using Page Templates
  • 13. Twenty Twelve Live Example (Child Theme is available for download) WordCamp Nashville 2013, 20 April 2013 Page 13 of 15 Custom Layouts Without Using Page Templates
  • 14. Links and Resources • Downloads o WCNash13 TwentyTwelve Child Theme o http://github.com/chipbennett/wcnash13-twentytwelve-child o Oenology Theme (advanced layout example) o http://github.com/chipbennett/oenology • Codex References o http://codex.wordpress.org/Pages#Page_Templates o http://codex.wordpress.org/Function_Reference/get_post_meta o http://codex.wordpress.org/Function_Reference/update_post_meta o http://codex.wordpress.org/Function_Reference/add_meta_box o http://codex.wordpress.org/Plugin_API/Filter_Reference/body_class • Presentation Slides o http://www.slideshare.net/chipbennett WordCamp Nashville 2013, 20 April 2013 Page 14 of 15 Custom Layouts Without Using Page Templates
  • 15. Feedback • Questions or comments? WordCamp Nashville 2013, 20 April 2013 Page 15 of 15 Custom Layouts Without Using Page Templates