I spend a lot of time working with WordPress, using it for simple blogs and also as a CMS solution for client sites. Some of my most used code snippets and tricks I have listed below.
Get your blog URL
<?php bloginfo('url'); ?>
Linking to an image/file (not from CSS file)
In this case you will need the URL to your theme files:
<?php bloginfo('template_directory'); ?>
So for example, if your logo image is in your theme directory “/juliaholland/images” your code would then read
<img src="<?php bloginfo('template_directory'); ?>/images/logo.gif" />
Find out if you are on the home page and set the ID for the div
<body<?php if(is_home()) { echo ' id="home"'; } ?>>
Navigation – Manual
Most sites that use WP as the CMS have navigation tabs/links that require an “active” state so that the user knows what page they are on.
An example of how this works:
<ul id="main-nav"> <li id="blog"><a href="/"<?php if(is_home() || is_single() || is_category() || is_tag() || is_author() || is_year() || is_month()) { echo ' class="active"'; }?>>Blog</a></li> <li id="about"><a href="/about/"<?php if(is_page('About')) { echo ' class="active"'; } ?>>About</a></li> <li id="contact"><a href="/contact/"<?php if(is_page('Contact')) { echo ' class="active"'; }?>>Contact</a></li> </ul>
Navigation – Automatic
In this case, the navigation links will be automatically generated by your pages (top level).
<ul id="main_nav"> <li class="home<?php if(is_home() || is_single() || is_category() || is_tag() || is_author() || is_year() || is_month()) { echo ' current_page_item'; }?>"><a href="<?php bloginfo('url'); ?>" title="Blog"><?php _e('Blog'); ?></a></li> <?php wp_list_pages('title_li=&depth=1'); ?> </ul>
Multiple Sidebars
This is particularly useful when you have several page templates and want to show different information in each of those sidebars.
<?php get_sidebar('home'); ?>
Where sidebar-home.php is the name of your file.
Using Improved Include Page
This plugin allows you to include different WP pages in one single page template. For example in your home template you might want to have different sections that can be edited from WP admin but aren’t all included in the same WP page.
Simply add
<?php iinclude_page(4); ?>
to your home page template and the content from your page 4 will be included.