Here we have the latest post on how I built the dyadica.net Paper Trail. Following on from yesterday the next thing needed to be done was convert the code into functions to aid with reciprocation / iteration and facility for multi functionality eg:
- Page Paper Trail
- Site Map
- Standard Breadcrumb Trail
This also provided opertunity to tidy up some of the code. The first pass came out as follows:
<?php
function pollPath($start , $child) {
$paths = explode(“/”, $_SERVER['REQUEST_URI']);
$count = (sizeof($paths)-1);
$trail = “/”;
$i = 1;
while ( $i <= $count ) {
$ptitle = $paths[$i];
$pURL = $trail.$ptitle;
$class = pollClass($i , $count);
echo “<li class=”.$class.”><a href=”.$start.$pURL.”>”.$ptitle.”</a></li>”;
$trail = $pURL.”/”;
$i = $i + 1;
} // end while
} // end pollPath
?>
This first block, function pollPath(), handles the separation of the current URI into a html list. Classes are applied to each link via a call to the pollClass() function which returns a $class variable depending upon the iteration value. pollClass() can be seen below:
<?php
function pollClass($i , $count) {
if ($i == ($count-1)) {
$class = “trail-parent”;
} elseif ($i == ($count)) {
$class = “trail-current”;
} else {
$class = “trail-item”;
}
return $class;
}
?>
The children for a specific page are then outputted by the following function. At this stage the code still uses the wordpress bespoke wp_list_pages() function to display the child pages.
<?php
function displayChildren($kID) {
$children = wp_list_pages(‘title_li=&child_of=’.$kID.’&echo=0&depth=1′);
if ($children) { echo $children; }
}
?>
The final function initiates the code by calling the pollPath() and displayChildren() functions.
<?php
function buildPageTrail($start , $startName, $pID, $data) {
echo “<ul class=’paper-trail’>”;
echo “<li class=’trail-root’><a href=”.$start.”>”.$startName.”</a></li>”; // hard coded root vars passed in
pollPath($start , $startName);
displayChildren($pID);
echo “</ul>”;
}
?>
The buildPageTrail() function above is initiated by the following call:
<?php buildPageTrail(“http://blog.dyadica.net”, “root”, $post->ID, $data); ?>
Properties within the call can be detailed as follows:
- “http://blog.dyadica.net” – Website URL
- “root” – Title
- $post->ID – ID of the current post.
- $data – Reference to the wordpress database.
<div style=”margin:10px; clear:both;”>
<?php $data = $wpdb; ?>
<img alt=”permalink image” class=”permalink” src=”<?php bloginfo(‘template_directory’); ?>/images/permalink.gif” />
<div class=”post-head”>papertrail</div>
<?php buildPageTrail(“http://blog.dyadica.net”, “root”, $post->ID, $data); ?>
</div>
Next thing I did is replace the wp_list_pages() call with a bespoke SQL call to the wordpress database, housed in a new function as follows:
<?php
function pollChildren($pID, $data) {
$kids = $data->get_results(“SELECT * FROM wp_posts WHERE post_parent=”.$pID);
if ($kids) {
foreach ($kids as $kid) {
$pTitle = $kid->post_title;
$pURI = str_replace(” “, “-”, $_SERVER['REQUEST_URI'].”/”.$pTitle);
$pURI = strtolower($pURI);
echo “<li><a href=’”.$pURI.”‘>”.$pTitle.”</a></li>”.”<br />”;
}
}
}
?>
say what do you think