permalinks and categories

2 posts by 1 authors in: Forums > CMS Builder
Last Post: February 16, 2019   (RSS)

By moh3 - February 16, 2019 - edited: February 16, 2019

In case someone is looking to utilize the permalinks along with categories structure or in case you have pages as well that have categories assigned to it here is how it works:

this is an important option to have in case you have a product catalog or a blog with multilevel categories

1- in the permalinks file find the function : permalink_generate();

2- inside the if ($permalinkText) { } condition add the following, my category table is called (blog_category) i'm checking if i'm in there in order to not interfere with the default code for other sections.

prerequisite : you have to create a url prefix in your permalink field as default value , in my case it was : /cms/blog/ , and anything afterwards is assigned automatically

The code :

Add a variable :

$specialname =''; // this important for adding the structure 

if( $_REQUEST['menu'] == 'blog_category' ) {

// Get the parent of the newly created category

$thisParent = $recordOrRequest['parentNum'];

// we need to have a query to read the bread crumbs, so we select the parent category //
$parentCat = mysql_select('blog_category', "num = '$thisParent' LIMIT 1");

if( $parentCat AND $thisParent != '0' ) { // check that this category is a child and the query is not empty

$parentName = $parentCat[0]['name'];
$catbreadCrumbs = $parentCat[0]['breadcrumb'];

// loop over the bread crumbs in order to read the complete structure of the categories ( blog/parent/child/sub-child )

foreach ( preg_split("/\s*:\s*/", $catbreadCrumbs) as $fragment ) {
$specialname .= _permalink_generate_formatInputText($fragment).'/';
}

$specialname = rtrim($specialname,'/');

// the magic is here our structre is concatenated to after the prefix and before the permalinktext 
$prefixedPermalink = $prefixPermalinkWith.$specialname.'/'.$permalinkText;

}

} 

else if( $_REQUEST['menu'] == 'blog' ) {

// get article category

$thisParent = $recordOrRequest['category'];
$parentCat = mysql_select('blog_category', "num = '$thisParent' LIMIT 1");

if( $parentCat AND $thisParent != '0' ) {

$parentName = $parentCat[0]['name'];
$catbreadCrumbs = $parentCat[0]['breadcrumb'];

foreach ( preg_split("/\s*:\s*/", $catbreadCrumbs) as $fragment ) {
$specialname .= _permalink_generate_formatInputText($fragment).'/';
}

$specialname = rtrim($specialname,'/');
$prefixedPermalink = $prefixPermalinkWith.$specialname.'/'.$permalinkText;

}

}

else {
$prefixedPermalink = $prefixPermalinkWith . $permalinkText; // normal permalink code or standard
}