Last time we left off with the original script being separated into functions resulting in a new script with similar functionality. Today we are going to focus on a little more cleaning and the addition of a new if statement and change to the SQL statement, to handle attachment posts (uploads eg images etc).
$kids = $data->get_results(“SELECT * FROM wp_posts WHERE post_parent=”.$pID);
The current SQL call above, caters for all (*) information within the respective tables to be gathered. Whilst this can be useful it really is a bit overkill and not very efficient. As all we have really needed up to this stage is the post_title and ID data this can be rationalized as follows.
$kids = $data->get_results(“SELECT ID, post_title FROM wp_posts WHERE post_parent=”.$pID);
However due to the archiving structure implemented within wordpress attachments are also outputted by the current version of Paper Trail, as they are also stored via the post table. To deal with this we need an additional chunck of information, the post_type. With this added the call now looks as follows:
$kids = $data->get_results(“SELECT ID, post_title, post_type FROM wp_posts WHERE post_parent=”.$pID);
With this now sorted an additional if statement can be added to the code to separate this “unwanted” data eg:
if ( $pType == “page” || $pType == “post” ) {
echo “type: “.$pType.”<br />”;
} else {
echo “dont show me”;
} // [$pType check, if]
Ok, simple enough however what this also does is provide another use for the Paper Trail script. Now we have the ability to separate and output attachment data if required. Cool! (I smell another script function, lol)
Read..
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. Read..
As this site progresses towards its next stage of completion need has arisen for a simple breadcrumb trail to be created, primarily for navigation within (wordpress) pages and articles.
Little did I know this would lead to something much greater which I have dubbed: The Paper Trail.
I started out by using the f$_SERVER['REQUEST_URI']; command to obtain the current page address. I then explode the result at each “/” to give me a list of individual pages. Then by looping through this list and incrementing the path value each time with the previous result I can construct paths to each of the respective sections.
The first pass looked like this:
<?php $pth = $_SERVER['REQUEST_URI']; ?>
<?php $paths = explode(“/”, $pth); ?>
<?php $trail = “”; ?>
<ul>
<?php foreach($paths as $path) : ?>
<?php $ptitle = $path; ?>
<?php $pURL = $trail.$ptitle.”/”; ?>
<li><a href=”<?php echo bloginfo(‘url’).$pURL; ?>”><?php echo $ptitle; ?></a></li>
<?php $trail = $pURL; ?>
<?php endforeach; ?>
</ul>
I then decided to modify the above to use a while loop to provide facility for control at various steps of the iteration if needed. Read..