Permalinks & Categories (breadcrumbs?)
5 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: May 15, 2013 (RSS)
By Toledoh - March 22, 2013
Hey Guys,
Out of interest, can we breadcrumb permalinks automatically?
ie. If I have a page.php that displays the details of a product x - I can have a permalink of /products/product-x/ which is great.
Can I get /products/category/product-x/ where "category" is the category of that product (so I can't hard-code the permalink "default" field)
Cheers,
Tim (toledoh.com.au)
By gregThomas - March 25, 2013
Hi Tim,
This isn't a feature that is built into the plugin, but I've created a simple function that can create the permalinks dynamically for you:
function createPermaLinks($permaUrl, $currentUrl){
$isAlreadyRecord = mysql_count('_permalinks', "permalink = '$permaUrl'");
if(!$isAlreadyRecord){
$insertArray = array(
'updatedDate' => date('Y-m-d H:i:s'),
'permalink' => $permaUrl,
'customSourceUrl' => $currentUrl,
'tableName' => '',
'recordNum' => '0',
'Old' => '0',
);
mysql_insert('_permalinks', $insertArray);
}
}
Here is an example of how to use it:
//Get the num value from the URL
$num = intval($_REQUEST['num']);
// load record from 'blog', this is used to display the blog records details on the page.
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'where' => "num = $num",
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$blogRecord = @$blogRecords[0]; // get first record
if (!$blogRecord) { dieWith404("Record not found!"); } // show error message if no record found
//Perma link creation function
function createPermaLinks($permaUrl, $currentUrl){
//Check a record for the permalink address doesn't already exist
$isAlreadyRecord = mysql_count('_permalinks', "permalink = '$permaUrl'");
//If it doesn't add a record for it to the database.
if(!$isAlreadyRecord){
$insertArray = array(
'updatedDate' => date('Y-m-d H:i:s'),
'permalink' => $permaUrl,
'customSourceUrl' => $currentUrl,
'tableName' => '',
'recordNum' => '0',
'Old' => '0',
);
mysql_insert('_permalinks', $insertArray);
}
}
//Get all of the blog records
$blogs = mysql_select('blog');
//Cycle through the blog records
foreach($blogs as $blog){
//Create your permalink url. I've used the blog category and title,
$permaUrl = urlencode($blog['category'])."/".urlencode($blog['title']);
//Create the actual url for the page.
$currentUrl = 'blog.php?num='.intval($blog['num']);
//Create the permalink if required.
createPermaLinks($permaUrl, $currentUrl);
}
?>
So mysql_select collects all of the blog records, then a foreach loop is used to cycle through them and create strings for the actual URL and permalink URL. These are added to the createPermaLinks function, which will create records for them in the permalinks section if required.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com
By Toledoh - March 25, 2013
Wow - that's for that. I'll give it a shot!
Tim (toledoh.com.au)
By Codee - May 15, 2013
Hi Greg,
Is this a one-time solution or is this code meant to be placed on a list page and run every time the page is accessed?
By gregThomas - May 15, 2013
Hi Equinox,
This is a one time solution that will convert links to a detail page into permalinks that could be run as a script. You could place it on a page to run every time a page it is accessed, but its fairly resource intensive, so it might slow the page down a bit.
Thanks
Greg
PHP Programmer - interactivetools.com