Feedback Follow Me on Twitter!

Multiple Wordpress Page Layouts in One Single Template

The Problem

Our client wants a medium-sized site he can edit using Wordpress as a CMS. Some sections in the navigation will have subpages, while some will not. We’re going to need a conditional that checks for subpages, and if they exist, uses a two-column layout…

subnav

…but if they don’t exist, removes the sidebar and uses a full-width <div>…

subnav

Multiple layouts can always be achieved with custom page templates, but to keep things clean and economical, it’s much better to rely on one global template (”page.php”) if you can get away with it. So how will it work?

The Solution

The first bit of code will set the “children” variable and determine which <div> class will wrap the content:

<div class=<?php if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>"narrowcolumn"<?php } else { ?>"widecolumn"<?php } ?>>

The second bit of code inserts a sidebar if necessary:

<?php if ($children) { ?>
<div id="sidebar">
In this section:
<ul><?php echo $children; ?></ul>
</div>
<?php } else { ?>
<?php } ?>

Bonus Challenge

Almost done, but wait — our client now wants just the “Resources” page to have a third column containing a list of useful sites:

subnav

Here we’ll use the is_page conditional to insert the column while the wp_list_bookmarks function grabs the links:

<?php if (is_page('resources')) { ?>
<div id="sidebar-resources"><?php wp_list_bookmarks(); ?></div>
<?php } else { ?>
<?php } ?>

The Final Template Code

With a few conditionals, our “page.php” template is now much smarter and can handle a variety of layouts:

<?php get_header(); ?>

<?php if (is_page('resources')) { ?>
  <div id="sidebar-resources"><?php wp_list_bookmarks(); ?></div>
  <?php } else { ?>
  <?php } ?>

<div class=<?php if($post->post_parent)
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) { ?>"narrowcolumn"<?php } else { ?>"widecolumn"<?php } ?>>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div class="post" id="post-<?php the_ID(); ?>">
  <h2><?php the_title(); ?></h2>
  <div class="entry">
  <?php the_content(); ?>
  </div>
  </div>
  <?php endwhile; endif; ?>
  <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
  </div>

<?php if ($children) { ?>
  <div id="sidebar">
  In this section:
  <ul><?php echo $children; ?></ul>
  </div>
  <?php } else { ?>
  <?php } ?>

<?php get_footer(); ?>

103 Responses {+}

Trackbacks

  1. Wp Wordpress » Blog Archive » Multiple Wordpress Page Layouts in One Single Template
  2. links for 2007-12-28 | NewsErrado
  3. Skylog » Blog Archive » links for 2007-12-29
  4. Weekly Links - December 29th | Vandelay Website Design
  5. Buffer Dump 29DEC07 « Feet up, eyes closed, head back
  6. Matt Herzberger.com » Blog Archive » Here is your del.icio.us goodness for 12-31
  7. Best Wordpress Beginner Articles and Worthy Plugins - Roundup
  8. Multiple Wordpress Page Layouts in One Single Template » Darren Hoyt Dot Com
  9. תבניות לוורדפרס » ITbananas
  10. 国外最好的wordpress初学者文章(原文) | 精华之家|HIGHTHOUSE
  11. WordPress Developer’s Toolbox | Developer's Toolbox | Smashing Magazine
  12. WordPress Developer’s Toolbox | Developer's Toolbox | Smashing Magazine
  13. WP开发者资源-2.8: WordPress的样式化 | 帕兰映像
  14. WordPress Developer’s Toolbox | rafdesign
  15. Wordpress Araçları - İngilizce | indirazzi.com BETA
  16. WordPress Developer’s Toolbox - 山歌好比春江水
  17. WordPress 开发者工具箱1 | Ruijin Ubuntu Blog 生活中的每一点点滴滴......
  18. WordPress 开发者工具箱
  19. 逐浪·网络 » Blog Archive » WordPress 开发者工具箱
  20. - WordPress 开发者工具箱(上)【联讯网】
  21. WordPress Developer’s Toolbox | Fusuy.com || Webmaster Accessary Platform
  22. cssframework » WordPress 开发者工具箱【完整版】
  23. Useful wordpress plugins and template tweaks | welcomebrand
  24. Top 50 Wordpress Tutorials - NETTUTS
  25. 50???WordPress?? at ??????
  26. Top 50 Wordpress Tutorials
  27. ???? _kavid.net » Blog Archive » ??????WordPress?50?????
  28. 50???WordPress?? | ???
  29. 50???WordPress?? | forcto.com
  30. 135+ Ultimate Round-Up of Wordpress Tutorials | About Us | instantShift
  31. 250 Wordpress Tutorials
  32. Top 50 Wordpress Tutorials | Design-Tut+
  33. 135+ Ultimate Wordpress Tutorials
  34. 1 Thing I ♥ About Work as a Wordpress Developer | Internet Marketing & Lifestyle
  35. 50个最强WordPress教程 | 帕兰映像
  36. 250 Wordpress Tutoriels et liens pour tous | Guppytrucs Freeware
  37. How to Create Multiple Page Layouts Using One Page Template in WordPress | Excelnet Media LLC
  38. WordPress的50个最强教程 | 四叶草水晶天堂
  39. tj mapes » Blog Archive » What I’m Reading (weekly)
  40. Blog – Velagapati - 300+ Resources to Help You Become a WordPress Expert
  41. Create your own Wordpress Themes with 20+ Tutorials | oOrch Blog
  42. WordPress Developer’s Toolbox | 9Tricks.Com - Tips - Tricks - Tutorials
  43. 50 Wordpress Tutorials « Uttammitra’s Blog
  44. WordPress 300+ Collection of Amazing Resources | Amazing and Inspiring Design
  45. 300+ Resources to Help You Become a WordPress Expert « eBSPAD
  46. 鄞高博客 » Blog Archive » 50个最强WordPress教程
  47. 300+ Resources to Help You Become a WordPress Expert | LeVoltz - iPhone Games, Engineering Projects, Wordpress Themes
  48. bulldoggie studio » Top 50 Wordpress Tutorials
  49.   WordPress Developer’s Toolbox  by Oshoamy
  50. 30+ Useful WordPress Tutorials | The Apple Tech Blog
  51. Top 50 Wordpress Tutorials from TutPlus | WordPress News Magazine
  52. 30+ Useful WordPress Tutorials
  53. 300多种可帮你成为WordPress专家的资源 | 上海网站建设,上海网络公司
  54. top 50 tutorials of wordpress(must use) | Umraz Ahmed | the official site
  55. Multiple Wordpress Page Layouts in One S… « wordpressmaster tutorial
  56. Client Requests vs Wordpress - Highwire Days
  57. Multiple WP Layouts in OneTemplate | Darren Hoyt Dot Com | Personal BLog
  58. Top 50 Wordpress Tutorials | NetExUrl | Web Development Tutorials & Design Resources
  59. 54 Awesome Tutorials To Satisfy The Appetite Of Any Web Developer | Tutorials | PelFusion.com
  60. Wordpress – Recursos para Webmasters | Tienda virtual online. Diseño de paginas web. Posicionamiento Seo y Sem
  61. Wordpress tutorials - Gurushala
  62. |会学习的狗狗| 50个最强WordPress教程 : Welcome to www.studydog.net
  63. 65 Of The Best WordPress Tutorials « Junkiee.Net
  64. 54 Awesome Tutorials for Any Web Developer | The Technology Reports
  65. Best of Wordpress Beginner Articles and Worthy Plugins – Roundup | Design Trip Blog
  66. WordPress Expert Resources, Wordpress Plugins, Wordpress Tutorials
  67. wordpress 资源收藏
  68. 300 resources to help you master WordPress « Er.Krushna Chandra Muni :: Professional Web Developer | Website Design Orissa | Website Design Bhubaneswar | Website Design India | Website Design New Zealand | Website Design Auckland
  69. Wordpressをカスタマイズする30のテクニック「30 WordPress Development Tutorials」 | WebLab
  70. 135 link cung cấp thủ thuật wordpress hot nhất | Giải Pháp SEO Internet Online Marketing-SEO consultants
  71. Ultimate Wordpress tutorials, themes and guides | Kroro.de - Robert Kropf
  72. 60+ Awesome WordPress Tutorials | The Best WP Themes Online
  73. 50 Ultimate Tutorials To Make You A Power Wordpress User | stylishwebdesigner
  74. 136 Massive Wordpress Tutorial Collection | WebCoreStudio
  75. 300多种可帮你成为WordPress专家的资源 | 眉山SEO-网络赚钱项目★网络创业培训★睿智推广
  76. Top Collection Of WordPress Tutorials On The Web | Nerdy Geeks Blogging Guide

Leave a Response