Monday, 5 January 2015
Create a Breadcrumb Trail II
Last week I wrote about creating a breadcrumb trail, explaining how to do this for your front page, static Page and single post page. This leaves us with the category page and archives (day, month and year).
Category Page
The category page is quite similar to the single post page code that we saw last week, except that we use the single_cat_title() function along with the get_cat_id function to get the category ID of the page we’re on (get_the_category() wouldn’t work for category pages, as it’ll pick up all categories for the first post on the page and can potentially mess things up!) ie.
[sourcecode language=”php”]if (is_category()) :
$pdata = get_cat_id( single_cat_title(“”,false) );
$data = get_category_parents($pdata, TRUE, ‘ » ‘);
$trail .= ” » “.substr($data,0,-9);
endif;[/sourcecode]
The Archives
The archive pages can be for a day, month or year, so we need to cover all 3 potential options. Note, to reduce a database call I’ve used paths relative to the root so you may need to edit these if your WordPress install is within a directory.
Day Archives
For the Day archives we want it in the format of Year » Month » Day, with the year and month being linked. To do this we can use the following code:
[sourcecode language=”php”]if (is_day()) :
$trail .= ” » “.get_the_time(‘Y’).””;
$trail .= ” » “.get_the_time(‘F’).””;
$trail .= ” » “.get_the_time(‘l’);
endif;[/sourcecode]
Here we check if the archive page is a daily archive page, if so then we use the tag get_the_time() to get the information about the archives. This tag accepts the various PHP Dateformatting to determine what to display. Here I’ve used the Y to get the full 4 digit year eg. 2009, then for the month archive link I’ve used Y/m which would insert 2009/02 into our link. I’ve set the month anchor text however to use ‘F’ which will display the name in full.
Finally I’ve then set the day to display as its name in full, using the ‘l’ (lowercase L). This isn’t linked as you’re already on the page for thisThis will then give an output such as
Home»» February» Sunday
Month and Year Archives
For the Month and Year Archives we just adapt the above code for the day, slowly removing the additional ‘crumb’ at the end of the output, so for these two archives we use
[sourcecode language=”php”]if (is_month()) :
$trail .= ” » “.get_the_time(‘Y’).””;
$trail .= ” » “.get_the_time(‘F’);
elseif (is_year()) :
$trail .= ” » “.get_the_time(‘Y’);
endif;[/sourcecode]
Final Code
So our final function code, including the code from last week, should now be:
[sourcecode language=”php”]function write_breadcrumb() {
$pid = $post->ID;
$trail = “ Home“;
if (is_front_page()) :
// do nothing
elseif (is_page()) :
$bcarray = array();
$pdata = get_post($pid);
$bcarray[] = ” » “.$pdata->post_title.”\n”;
while ($pdata->post_parent) :
$pdata = get_post($pdata->post_parent);
$bcarray[] = ” » “.$pdata->post_title.”\n”;
endwhile;
$bcarray = array_reverse($bcarray);
foreach ($bcarray AS $listitem) :
$trail .= $listitem;
endforeach;
elseif (is_single()) :
$pdata = get_the_category($pid);
$data = get_category_parents($pdata[0]->cat_ID, TRUE, ‘ » ‘);
$trail .= ” » “.substr($data,0,-8);
elseif (is_category()) :
$pdata = get_cat_id( single_cat_title(“”,false) );
$data = get_category_parents($pdata, TRUE, ‘ » ‘);
$trail .= ” » “.substr($data,0,-9);
elseif (is_day()) :
$trail .= ” » “.get_the_time(‘Y’).””;
$trail .= ” » “.get_the_time(‘F’).””;
$trail .= ” » “.get_the_time(‘l’);
elseif (is_month()) :
$trail .= ” » “.get_the_time(‘Y’).””;
$trail .= ” » “.get_the_time(‘F’);
elseif (is_year()) :
$trail .= ” » “.get_the_time(‘Y’);
endif;
return $trail;
}
[/sourcecode]
The above code uses a right angled quote to separate the links however the most suitable markup for this is to use a list (except WordPress didn’t want me to paste that into the post!) so if you would rather use a list you can download the complete function, and just insert it into your functions.php file.
Remaining Pages
You may think, what about the other pages – tags, author pages and search results. I’ve not done this as the only page ‘above’ them is the home page, however if you wanted to add them in then you could use the correct conditional for each and just extend the if else statement above. These conditionals are is_tag(), is_author() and is_search().
TheoBloggingtips com
No comments: