Chief Alchemist - Business Consulting For The 21st Century Via A Holistic & Intelligent Approach
Share ChiefAlchemist.com.RSS 2.0 feed for ChiefAlchemist.com.Sign up for the mailing list.Follow Chief Alchemist on Twitter.'Like' the Chief Alchemist's page on Facebook.See what Chief Alchemist has been Q&A'ing on Quora.Chief Alchemist bookmarks & highlights on Diigo.com.Follow the Chief Alchemist on Digg.com.Chief Alchemist channel on Last.fm.Chief Alchemist on Flickr.Mark 'Chief Alchemist' Simchock on LinkedIn.Free Initial Consultation.Email Chief Alchemist.Phone Chief Alchemist.
  • Mark ‘Chief Alchemist’ Simchock
  • 'Email me.Email => ca .at. ChiefAlchemist .dot. com
  • 'Phone me.Alchemy United => 732 997-0028
  •  
  • Free initial consultation.Free => Initial Consultation
  • Please be sure to subscribe to your communication channels of choice.
  • Click To Close => The small green (consultation), red (email) or blue (phone) icons in the top upper right.
CURRENT “TOP 10” TOPICS TOOLS CLIENTS & PROJECTS SOURCES SEARCH HIDE
Business Consulting For The 21st Century Via A Holistic & Intelligent Approach

WordPress Taxonomy Functions – The Remix

I’ve made some additional enhancements to the custom functions I’ve added to this WP theme. These functions are for displaying the taxonomy terms and associated meta data. The  WordPress plug-in I’m using to manage the taxonomies from the admin side is:

http://wordpress.org/extend/plugins/simple-taxonomies/

There are a couple things to note about this plug-in:

1) Be careful about the taxonomy slugs. Once you set and save them they can not be changed via the plug-in.  It looks like you can but you can not – at least I wasn’t able to so. I had to make the changes manually in the database.

2) There seem to be times that the permalinks need to be refreshed. You’ll have to change your perrmalink option, save, change it back to what you want, and save again.

3) I also ran into some issues with deleting terms. Again, I had to work directly with the DB. However, I’m not sure if this is a plug-in issue or a quirk in WordPress.

Other than these bumps in the road, adding taxonomies to the tool box has been enlightening and ultimately well worth the effort. They allow for a much more powerful UX. I’m official, I’m a taxonomy evangelist. You can kiss your tags goodbye.

Now for the custom  functions that make it all happen. You’ll have to excuse my naming convention, it’s somewhat unique. Yes, it’s a bit heavy handed but ultimately, I’m just trying to make it easy for anyone, including myself weeks from now, to read, understand and modify. Copy and paste these into your theme’s function.php.

1) funct_PrepareTaxAndTerms ( ‘Pass a String of Taxonomies to be Included’ ) – The call to this function is added to the page header. For the specified taxonomies it returns two arrays. One array is filled using WordPress get_terms. The other array has the terms’ description with the taxonomy+term pair as the index/key. Both of these arrays get used down the page so they’re created once in advance. This is what’s pasted in the header.php
<?php
global $arr_DescriptionsKeyedBySlugs, $arr_IncludeAllTaxTerms;
// What taxonomies? Required!!!
$str_IncludeTaxList = 'topic,client-project,tool,source';
extract(funct_PrepareTaxAndTerms($str_IncludeTaxList));
$arr_DescriptionsKeyedBySlugs = $arr_DescriptionsKeyedBySlugsFunct;
$arr_IncludeAllTaxTerms = $arr_IncludeAllTaxTermsFunct;
?>

2) funct_URITaxDescript() – For the page it’s rendered on it returns the description for the taxonomy+term in the URI. This I used on the archive pages. For example, Topic: WordPress Customization (topic/wordpress-customization) is an archive page. I also added the function to the All In One SEO Pack plug-in so the term description would also appear in the meta title on the archive page. Titles are important to SEO so adding the descriptions might help.

3)  funct_PostTaxSorted ( Post ID , Separator, String Before, String After,  Include Or Exclude,  String of Taxonomies,  Add Title = Term Description) – This function lists the taxonomies for a given post. It’s more or less a tricked out version of the standard WordPress get_the_term_list function. You’ll have to paste the call to this function where your theme lists posts and you want to display the taxonomy terms for each post.

— Post ID (Required, Integer) – As defined by WP

— Separator (Required, String) – What appears between the terms as they’re listed.  For example, this site uses ‘ . ‘. That is, quote space period space close-quote.

— String Before (Required, String) – What appears before the term. For example, an opening span tag. If you don’t want to specify anything just pass ”. That is, quote no-space close-quote).

— String After (Required, String) – What appears after the term. For example, a closing span tag. If you don’t want to specify anything just pass ”.

— Include Or Exclude (Required, String) – Values: ‘IN’ or ‘EX’. That is, INclude or EXclude the taxonomies in the String of Taxonomies parm that follows.

— String of Taxonomies (Required, String) – Comma separated list. Allows you to specify which taxonomies are to be listed (in the event you don’t want to list them all). Example: ‘mytax1,mytax2,mytax4′.

— Add Title = Term Description (Required, String) – Values: “YES’ or ‘NO’. The standard WordPress get_the_term_list function does not make the links’ title = “term description”. This does.

=> Here’s the actual code that pasted into the theme’s function.php:
// We used the taxonomies and terms quite a bit down the page so let's load them into arrays now.
// Load once, use many
function funct_PrepareTaxAndTerms($str_IncludeTaxList)
{
// Convert the parm str_ing in an array
if (isset($str_IncludeTaxList)) {
$arr_IncludeTaxList = explode(',',$str_IncludeTaxList);
}
// get the terms based on the $arr_IncludeTaxList.
// Reminded: $arr_IncludeAllTaxTermsFunct is returned from the function and should then assigned
// to a the global variable. In this case, $arr_IncludeAllTaxTerms to be used further down the page
//
if (!empty($arr_IncludeTaxList)) {
$arr_IncludeAllTaxTermsFunct = get_terms($arr_IncludeTaxList);
} else { // if arr_ay is empty then default to get all terms for all taxonomies
$arr_IncludeAllTaxTermsFunct = get_terms();
}
//
// get_terms object for the taxonomies
foreach ($arr_IncludeAllTaxTermsFunct as $row_IncludeTaxTerms) {
// is the term description is empty then default to the name for the description
$str_descript = $row_IncludeTaxTerms->name;
if (!empty($row_IncludeTaxTerms->description)) {
$str_descript = $row_IncludeTaxTerms->description;
}
// make an array key index out of the taxonomy + / + slug
$arr_KeySlug = $row_IncludeTaxTerms->taxonomy.'/'.$row_IncludeTaxTerms->slug;
// put the term description into the array using key index
// Reminded: $arr_DescriptionsKeyedBySlugsFunct is returned from the function and is then assigned to
// the global $arr_DescriptionsKeyedBySlugs to be used further down the page
$arr_DescriptionsKeyedBySlugsFunct[$arr_KeySlug] = $str_descript;
}
return compact('arr_DescriptionsKeyedBySlugsFunct', 'arr_IncludeAllTaxTermsFunct');
}
//
// This function deconstructs the current URI by parsing out the current taxonomy and term
// And uses $arr_DescriptionsKeyedBySlugs to return the description for that tax+term key seen in the URI
//
// ADD THIS LINE OF CODE WHERE YOU WANT THE TERM DESCRIPT TO APPEAR
// $date = $date . ' - '. funct_URITaxDescript(); << this is in the archive section of All in One SEO
//
// function funct_URITaxDescript() {
global $wp_taxonomies, $arr_DescriptionsKeyedBySlugs;
// get the REQUEST_URI
$str_ArchiveURI = $_SERVER['REQUEST_URI'];
// explode that string into an array to make it easy to deal with
$arr_ArchiveURI = explode ('/',$str_ArchiveURI);
// Since there's a leading slash the [0] is blank. Element [1] is the taxonomy we want to get the terms for. Element [2] is the specific term
if ( (!empty($arr_ArchiveURI[1])) && (!empty($arr_ArchiveURI[2]))) {
$str_MakeKey = $arr_ArchiveURI[1].'/'.$arr_ArchiveURI[2];
$str_TheDescript = $arr_DescriptionsKeyedBySlugs[$str_MakeKey];
} else {
$str_TheDescript = 'UNKNOWN';
}
return $str_TheDescript;
}
//
// ----
// For the current post, get the terms. Returns an HTML string. You may INclude of EXclude particular taxonomies.
function funct_PostTaxSorted($parm_PostID, $separator,$str_Before,$str_After, $parm_IncludeOrExclude, $str_InExArray, $parm_AddTitleDescript)
{
global $wp_taxonomies,$arr_DescriptionsKeyedBySlugs;
if (isset($str_InExArray)) {
$arr_InExArray = explode(',',$str_InExArray);
}
$str_PostTaxList = '';
if (($parm_IncludeOrExclude == 'IN') && (isset($arr_InExArray))) {
foreach ($wp_taxonomies as $taxonomy) {
// INclude the terms of the taxonomies listed in this array
if ( in_array($taxonomy->name, $arr_InExArray ) == true ) {
$str_PostTaxList .= get_the_term_list($parm_PostID, $taxonomy->name,'','|','|');
}
}
} elseif (($parm_IncludeOrExclude == 'EX') && (isset($arr_InExArray))) {
foreach ($wp_taxonomies as $taxonomy) {
// EXclude the terms of the taxonomies listed in this array
if ( in_array($taxonomy->name, $arr_InExArray ) != true ) {
$str_PostTaxList .= get_the_term_list($parm_PostID, $taxonomy->name,'','|','|');
}
}
} else // Use All {
foreach ($wp_taxonomies as $taxonomy) {
$str_PostTaxList .= get_the_term_list($parm_PostID, $taxonomy->name,'','|','|');
}
}
//
if (isset($str_PostTaxList)) { // trim off any blanks as well as the extra | at the end of the string. For some odd reason rtrim didn't want to strip off the extra |
trim('$str_PostTaxList');
$str_PostTaxList = substr($str_PostTaxList,0,(strlen($str_PostTaxList)-1));
//
// Convert the string into an array
$arr_PostTaxList = explode('|',$str_PostTaxList);
$str_StripTags = '';
$arr_ListByTaxKey = array();
// build a new array using (what is ultimately) the taxonomy->name as the array key
foreach ($arr_PostTaxList as $str_PostTaxSingle) {
$str_StripTags = strip_tags($str_PostTaxSingle);
$arr_ListByTaxKey[$str_StripTags] = $str_Before.$str_PostTaxSingle.$str_After;
}
// sort the new array on the key
ksort($arr_ListByTaxKey);
//
// take the sorted array and convert it back into a string of HTML. Add the title attribute with the description as the value while you're at it.
$str_PostTaxListSorted = '';
foreach ($arr_ListByTaxKey as $str_TaxKeySingle) {
if ($parm_AddTitleDescript == 'YES') // Add the title="term->description" to the HTML? {
// some string parsing trickery in order to insert the term description as the title into the post's taxonomy list
$int_ParseOutSlugStart = stripos($str_TaxKeySingle,'.com/') + 5;
$int_ParseOutSlugEnd = stripos($str_TaxKeySingle,'/" rel') ;
$str_Slug = substr( $str_TaxKeySingle, $int_ParseOutSlugStart, ($int_ParseOutSlugEnd - $int_ParseOutSlugStart));
$str_HTMLtitle = ' title="'.trim($arr_DescriptionsKeyedBySlugs[$str_Slug]).'" rel=';
$str_TaxKeySingle = preg_replace ('/ rel=/', $str_HTMLtitle, $str_TaxKeySingle);
}
$str_PostTaxListSorted = $str_PostTaxListSorted.$str_TaxKeySingle.$separator;
}
//
// remove the extra separator that was appended to the end
$str_PostTaxListSorted = substr($str_PostTaxListSorted,0,(strlen($str_PostTaxListSorted)-strlen($separator)));
}
return $str_PostTaxListSorted;
}

Required.
Will not be published. Required.
Please include http://