Out of the box WordPress comes as a useful tool for creating simple brochure websites. The mass of community plugins also add real value to WordPress as a platform for building simple websites. When creating bespoke templates there are a few useful code snippets, which I have listed some of below, that come in handy when coding specific features into your PHP template files within your theme.
1. Embedding a Menu Snippet into a template
This piece of code enables you to put a custom menu anywhere into your templates:
><?php wp_nav_menu( array('menu' => 'Homepage Links' )); ?>
(where ‘Homepage Links’ is the NAME of your custom menu)
2. Creating a new widget region
To create a new widget region that you can place widgets in you need to edit your functions.php file AND then the template file you want to place the region into. Firstly in functions.php you need to add:
register_sidebar( array( 'name' => __( 'Homepage News', 'twentyten' ), 'id' => 'news', 'description' => __( 'News section on homepage', 'twentyten' ), 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', 'after_widget' => '</li>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) );
This adds a new region called ‘Homepage News’, in this case within the twentyten template structure. The before and after widget code enables you to add custom styles and HTML tags before and after each widget placed within the region.
Then, within your template file (i.e. sidebar.php) where you want it to appear you need to place the region:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Homepage News") ) : ?><?php endif; ?>
You are now free to add widgets to the region and they will appear on your site.
3. The template directory
<?php bloginfo('template_directory'); ?>