WordPress breadcrumb navigation PHP code

Here’s some really simple PHP code to generate breadcrumb navigation in WordPress. It only supports navigation down two levels – i.e. you have a page that defines a section of a site, and then child pages within the section. It doesn’t support grandchild or deeper navigation.

The code is laid out in a really simple way, so you can see exactly how it works. Just place the following code in your QWordPress template functions.php file:

function get_breadcrumb() {
global $wp_query;
echo(‘<a href=”‘);
bloginfo(‘siteurl’);
echo(‘”>Home</a>’);
if(!is_home()) {
echo(‘ &raquo; ‘);
if (!empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->post_parent;
echo(‘<a href=”‘);
echo(get_permalink($parent));
echo(‘”> ‘);
echo(get_the_title($parent));
echo(‘</a> &raquo; ‘);
}
}
echo(‘<a href=”‘);
get_permalink();
echo(‘”>’);
the_title();
echo(‘</a>’);
}

To generate the breadcrumbs, just use

get_breadcrumb();

within any of the php files for your template.

WordPress PHP code for sidebar with dynamic content determined by current page

Here is some really simple PHP code that I wrote for a client’s WordPress site. They wanted to have a sidebar that pulled different content depending upon the section of the website. Their site has some “category” pages, with child pages that contain more information (e.g. the Products page has a child page that describes each of the main products the company offers in more detail).

So wrote some PHP to determine the section, which is defined by the top level page ID, and then calls the appropriate PHP files to insert the right content. It’s easy to modify the include commands to pull in different content, depending upon the needs of your site.

post->ID;
$menuparent = $post->post_parent;
if ($menuparent == 0) {
$menuparent = $thePostID; }
/* now work out where we are */
switch ($menuparent) {
case 14: /* products section */
include(TEMPLATEPATH.”/sidebar-customer-quotes.php”);
include(TEMPLATEPATH.”/sidebar-find-out-more.php”);
break;
case 30: /* solutions section */
include(TEMPLATEPATH.”/sidebar-customer-quotes.php”);
include(TEMPLATEPATH.”/sidebar-find-out-more.php”);
break;
case 47: /* services section */
include(TEMPLATEPATH.”/sidebar-customer-quotes.php”);
break;
case 60: /* news section */
include(TEMPLATEPATH.”/sidebar-events.php”);
break;
default: /*not sure which page – not 1st or second level */
echo(‘this is the wrong template for page ID # ‘.$thePostID);
}
?>

PHP code for WordPress side menu

I’ve been developing a website for a client who wanted a dynamic side menu. The site is structured, with top level menu items acting as the parent page of second level items. So for example “About Us” is a main menu item, with no parent. The “Management” page, however is a child of “About Us”.

The client wanted a side menu that showed the parent page at the top of the list, with the child pages beneath. The current page needed to be highlighted in red, and they also wanted the menu items to be highlighted in red when you mouse over them.

So I needed to generate a menu that worked out if you were on a child or top level page, and then pull the appropriate pages into a list. Here’s the PHP code that I wrote to create the side menu:

post->ID;
$menuparent = $post->post_parent;
if ($menuparent == 0) {
$menuparent = $thePostID; }
$menuargs = ‘title_li=&include=’.$menuparent;
wp_list_pages($menuargs);
$menuargs = ‘title_li=&child_of=’.$menuparent.’&depth=1&sort_column=menu_order’;
wp_list_pages($menuargs);
?>

And here is the CSS to do the formatting.

#leftmenu {
margin: 0px;
padding: 0px;
}

#leftmenu ul {
list-style: none;
margin: 0px;
padding: 10px 0px 0px 0px;
text-align: left;
text-decoration: none;
line-height: 1;
}

#leftmenu li, #leftmenu li a, #leftmenu li a:link, #leftmenu li a:visited {
color: #9A9C9D;
text-align: left;
text-decoration: none;
display: block;
padding: 4px 0px 4px 10px;
font-size: 14px;
text-indent: 0px;
}

#leftmenu li {
border-top-style: solid;
border-top-color: #9A9C9D;
border-top-width: 1px;
}

#leftmenu li a:hover {
color: #C62032;
}

#leftmenu li a:active {
color: #C62032;
}

#leftmenu .current_page_item a:link, #leftmenu .current_page_item a:visited {
color: #C62032;
}

I’m not a programmer so I’d love to hear some comments about the code or suggestions for improvement?