Creating Offsite Category Links
Submitted by jwheat on Fri, 11/08/2013 - 15:54
With our site, we have a top blue navigation bar. This is build using categories from the taxonomy. If you've used the taxonomy to build out your navigation links, you know that if you needed to link to an off site webpage you would have to add a Navigation Suppliment and tack it at the end if your links.
I came up with a neat little modification. The code I'll list is specific to our installation because we have our front-end scripts pulling different sets of categories to build different list of links. That said, I'm sure you'll be able to alter your code to allow for this functionality.
The thought here is to append a url to the category name and parse it out, and build the link using those two bits of data instead of having Jadu link it up internally.
I edited a category called 'Brief History' on our About page like this in the taxonomy :
Brief History|http://en.wikipedia.org/wiki/History_of_the_world
Save it, then published the new taxonomy (this is VERY important :)
In /site/includes/structure/mast.php we had a section of code that looked like this -
<?php
foreach ($secondLevelCategories as $subCat) {
?>
<li><a href="<?php print getSiteRootURL() .buildDocumentsCategoryURL($subCat->id) ?>">
<?php print encodeHtml($subCat->name); ?></a>
</li>
<?php
}
?>
This spins through the secondLevelCategories we have listed and builds out our blue 'sub' nav bar.
What I wanted to do was change how this link was rendered.
So I tossed in some code that looks like this -
<?php
foreach ($secondLevelCategories as $subCat) {
if (strpos($subCat->name, "|") !== false) {
# split category string on |
$mc_category_array = explode("|", $subCat->name);
$subCat_name = $mc_category_array[0];
$subCat_link = $mc_category_array[1];
?>
<li><a href="<?php print $subCat_link ?>">
<?php print encodeHtml($subCat_name); ?></a>
</li>
<?php
} else {
?>
<li>
<a href="<?php print getSiteRootURL() .buildDocumentsCategoryURL($subCat->id) ?>">
<?php print encodeHtml($subCat->name); ?></a>
</li>
<?php
}
}
?>
Let me explain all of that.
I check to see if the category name has the | delimiter in it, if not, I render it as normal. If it does, I explode it into an array. Set the [0] index to the link text, and the [1] index to the url, then assemble the <li> in to the format we want.
We have a policy to not include target="_blank" for any links because of accessibility reasons, otherwise you could toss one in that link.
Now when the sub nav renders on our page if we've defined a special url in the category name, i'll link off to that site.
General Concept:
Jadu Product:
Jadu Version:
