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…

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

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:

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(); ?>
Pingback: Wp Wordpress » Blog Archive » Multiple Wordpress Page Layouts in One Single Template
Pingback: links for 2007-12-28 | NewsErrado
Pingback: Skylog » Blog Archive » links for 2007-12-29
Pingback: Weekly Links - December 29th | Vandelay Website Design
Pingback: Buffer Dump 29DEC07 « Feet up, eyes closed, head back
Pingback: Matt Herzberger.com » Blog Archive » Here is your del.icio.us goodness for 12-31
Pingback: Best Wordpress Beginner Articles and Worthy Plugins - Roundup
Pingback: Multiple Wordpress Page Layouts in One Single Template » Darren Hoyt Dot Com
Pingback: ×ª×‘× ×™×•×ª לוורדפרס » ITbananas
Pingback: 国外最好的wordpressåˆå¦è€…æ–‡ç« ï¼ˆåŽŸæ–‡ï¼‰ | ç²¾åŽä¹‹å®¶|HIGHTHOUSE
Pingback: WordPress Developer’s Toolbox | Developer's Toolbox | Smashing Magazine
Pingback: WordPress Developer’s Toolbox | Developer's Toolbox | Smashing Magazine
Pingback: WPå¼€å‘者资æº-2.8: WordPressçš„æ ·å¼åŒ– | å¸•å…°æ˜ åƒ
Pingback: WordPress Developer’s Toolbox | rafdesign
Pingback: Wordpress Araçları - İngilizce | indirazzi.com BETA
Pingback: WordPress Developer’s Toolbox - å±±æŒå¥½æ¯”春江水
Pingback: WordPress å¼€å‘者工具箱1 | Ruijin Ubuntu Blog 生活ä¸çš„æ¯ä¸€ç‚¹ç‚¹æ»´æ»´......
Pingback: WordPress å¼€å‘者工具箱
Pingback: 逿µªÂ·ç½‘络 » Blog Archive » WordPress å¼€å‘者工具箱
Pingback: - WordPress å¼€å‘者工具箱(上)ã€è”讯网】
Pingback: WordPress Developer’s Toolbox | Fusuy.com || Webmaster Accessary Platform
Pingback: cssframework » WordPress å¼€å‘者工具箱ã€å®Œæ•´ç‰ˆã€‘
Pingback: Useful wordpress plugins and template tweaks | welcomebrand
Pingback: Top 50 Wordpress Tutorials - NETTUTS
Pingback: 50???WordPress?? at ??????
Pingback: Top 50 Wordpress Tutorials
Pingback: ???? _kavid.net » Blog Archive » ??????WordPress?50?????
Pingback: 50???WordPress?? | ???
Pingback: 50???WordPress?? | forcto.com
Pingback: 135+ Ultimate Round-Up of Wordpress Tutorials | About Us | instantShift
Pingback: 250 Wordpress Tutorials
Pingback: Top 50 Wordpress Tutorials | Design-Tut+
Pingback: 135+ Ultimate Wordpress Tutorials
Pingback: 1 Thing I ♥ About Work as a Wordpress Developer | Internet Marketing & Lifestyle
Pingback: 50个最强WordPress教程 | 帕兰映像
Pingback: 250 Wordpress Tutoriels et liens pour tous | Guppytrucs Freeware
Pingback: How to Create Multiple Page Layouts Using One Page Template in WordPress | Excelnet Media LLC
Pingback: WordPress的50个最强教程 | 四叶草水晶天堂
Pingback: tj mapes » Blog Archive » What I’m Reading (weekly)
Pingback: Blog – Velagapati - 300+ Resources to Help You Become a WordPress Expert
Pingback: Create your own Wordpress Themes with 20+ Tutorials | oOrch Blog
Pingback: WordPress Developer’s Toolbox | 9Tricks.Com - Tips - Tricks - Tutorials
Pingback: 50 Wordpress Tutorials « Uttammitra’s Blog
Pingback: WordPress 300+ Collection of Amazing Resources | Amazing and Inspiring Design
Pingback: 300+ Resources to Help You Become a WordPress Expert « eBSPAD
Pingback: 鄞高博客 » Blog Archive » 50个最强WordPress教程
Pingback: 300+ Resources to Help You Become a WordPress Expert | LeVoltz - iPhone Games, Engineering Projects, Wordpress Themes
Pingback: bulldoggie studio » Top 50 Wordpress Tutorials
Pingback: WordPress Developer’s Toolbox by Oshoamy
Pingback: 30+ Useful WordPress Tutorials | The Apple Tech Blog
Pingback: Top 50 Wordpress Tutorials from TutPlus | WordPress News Magazine
Pingback: 30+ Useful WordPress Tutorials
Pingback: 300多种可帮你成为WordPress专家的资源 | 上海网站建设,上海网络公司
Pingback: top 50 tutorials of wordpress(must use) | Umraz Ahmed | the official site
Pingback: Multiple Wordpress Page Layouts in One S… « wordpressmaster tutorial
Pingback: Client Requests vs Wordpress - Highwire Days
Pingback: Multiple WP Layouts in OneTemplate | Darren Hoyt Dot Com | Personal BLog
Pingback: Top 50 Wordpress Tutorials | NetExUrl | Web Development Tutorials & Design Resources
Pingback: 54 Awesome Tutorials To Satisfy The Appetite Of Any Web Developer | Tutorials | PelFusion.com
Pingback: Wordpress – Recursos para Webmasters | Tienda virtual online. Diseño de paginas web. Posicionamiento Seo y Sem
Pingback: Wordpress tutorials - Gurushala
Pingback: |会学习的狗狗| 50个最强WordPress教程 : Welcome to www.studydog.net
Pingback: 65 Of The Best WordPress Tutorials « Junkiee.Net
Pingback: 54 Awesome Tutorials for Any Web Developer | The Technology Reports
Pingback: Best of Wordpress Beginner Articles and Worthy Plugins – Roundup | Design Trip Blog
Pingback: WordPress Expert Resources, Wordpress Plugins, Wordpress Tutorials
Pingback: wordpress 资源收藏
Pingback: 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
Pingback: Wordpressをカスタマイズする30のテクニック「30 WordPress Development Tutorials」 | WebLab
Pingback: 135 link cung cấp thủ thuật wordpress hot nhất | Giải Pháp SEO Internet Online Marketing-SEO consultants
Pingback: Ultimate Wordpress tutorials, themes and guides | Kroro.de - Robert Kropf
Pingback: 60+ Awesome WordPress Tutorials | The Best WP Themes Online
Pingback: 50 Ultimate Tutorials To Make You A Power Wordpress User | stylishwebdesigner
Pingback: 136 Massive Wordpress Tutorial Collection | WebCoreStudio
Pingback: 300多种可帮你成为WordPress专家的资源 | 眉山SEO-网络赚钱项目★网络创业培训★睿智推广
Pingback: Top Collection Of WordPress Tutorials On The Web | Nerdy Geeks Blogging Guide
Pingback: zhq » Archive » 50个最强WordPress教程
Pingback: » 50个最强WordPress教程 @ Cloud 1989
Pingback: 50 полезных уроков для веб разработчика (часть 1)
Pingback: 50个最强WordPress教程 | Cloud 1989
Pingback: 50个wordpress basic | JavaShare
Pingback: 135 link cung cấp thủ thuật wordpress hot nhất | Tạp Chí SEO
Pingback: Some Great Wordpress Resources — we kill people
Pingback: Top Collection Of WordPress Tutorials On The Web | Nerdy Geeks
Pingback: 135 webs/blog for wordpress tips |
Pingback: 135 trang web/blog thủ thuật wordpress hay nhất | Ninh Bình 24h
Pingback: 精华分享网-分享互联网 » 50个最强国外WordPress教程
Pingback: 300多种可帮你成为WordPress专家的资源 _ 网赚_网络赚钱项目_网上兼职赚钱_SEO培训★月赚万元★金苹果网
Pingback: 60+ Awesome WordPress Tutorials » Themetation
Pingback: 50个最强WordPress教程 « 臭皮匠
Pingback: | Free Online Web Tutorials
Pingback: worldpress 教程大全 - wordpress-前沿体验frontexp.com
Pingback: 135 trang web/blog thủ thuật wordpress hay nhất | iTViệt.VN - itviet.vn - Cổng thông tin dành cho giới trẻ Việt Nam
Pingback: Wordpress教程大全 | 得失之间,谈笑自若
Pingback: 60+ Awesome WordPress Tutorials | Template Indonesia
Pingback: 300+ Resources to Help You Become a WordPress Expert | Kill your Time
Pingback: 20 Useful Tutorials For Creating WordPress Themes - Alexa Web DesignAlexa Web Design
Pingback: How To Create WordPress Themes Tutorial - Resources
Pingback: 菜博 » 50个wordpress最强教程
Pingback: 135 trang web/blog thủ thuật wordpress hay nhất « mockhangnoithat
Pingback: 135 TRANG WEB/BLOG THỦ THUẬT WORDPRESS HAY NHẤT « Carton Boxes Packing (bao bì Carton)
Pingback: wordpress开发教程。 | suliuer