Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-01-27 18:00:49

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Category URL (clean) in breadcrumbs?

Hello!
I long ago noticed that TXP breadcrumbs do not work perfectly on individial article page. They don’t show categories at all. Even such great plugins like etz_crumbs does not show categories Clean Urls. It does show them in the form of www.site.com/section/?=category. I wonder why is so? And is there any way to show clean category urls there?

Offline

#2 2015-01-27 19:39:19

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,596
Website

Re: Category URL (clean) in breadcrumbs?

I’ve no idea about etz_crumbs but in general the reason lies partly in the fact that textpattern’s sectioning and categorising principles exist parallel to one another rather than beneath each other. The standard clean category url is at root level, i.e. /category/category-name. As soon as it is used within a section, it reverts to messy urls, i.e. /section/?c=category-name because it would otherwise run the risk of clashing with a proper individual article url if they had the same name.

The plugin gbp_permanent_links does provide a way of constructing proper /section/category and even /section/category/url-title link names by intervening in the url handling process. It is a powerful plugin that takes some playing with to get right (due in part to a lack of documentation).

For simpler sites – where I know that I must avoid using the same name for a category and an article – I add a few lines to the .htaccess file to do this intervening for me. The disadvantage is it is a manual amendment that also needs updating should the category names change or new categories be added, so it’s not perfect for every case.

Example: for a recent site for an architecture office I add this above txp’s own rewrite rules in the .htaccess file:

# custom rewrite rules for project categories
# Needs updating if category names change or new categories are added

 RewriteRule ^(projects)/(conservation|commercial|housing|competitions)/?$ index.php?s=$1&c=$2 [L]

That gives you /projects/conservation instead of /projects/?c=conservation and so on. if you were worried about clashes, you could make it rewrite from ^projects/type/(category|category-b|category-c)/?$.

Note: You also have to construct your links in your templates manually, e.g. txp:category1 title="1" link="1" will give you the wrong link but making your own using txp:site_url /><txp:section />/<txp:category1 /> works…

– - -

Incidentally, another tricky case with breadcrumbs arose with the architecture office site:

Say a project can be classed in more than one category, e.g. in “housing” and in “competitions” projects, how do you do your breadcrumb view:

  • You could do Home » Projects » cat1 | cat2
  • You could do Home » Projects » Name and put the category back links elsewhere in a separate “filed under: cat1, cat2” element.
  • You could do Home » Projects » cat1 and leave it at that.
  • But what I wanted was: Home » Projects » Last-visited-cat » Name

In an old project many years ago, I solved this by setting a cookie in the category-list page one step up, and then retrieve that cookie on the individual article to set the correct category backlink. It works but costs a cookie, and produces wrong results if you happen to visit the page via another path or enter the url manually in the browser. You can mostly remedy that by judiciously deleting the cookie on other pages. Oh, and in the EU, you need to should okay the use of cookies with the visitor too …

This time I tried another approach using $_SERVER['HTTP_REFERER'] which holds the url of the page you have just come from. The tail end of that string is your category. Store the referer in a variable and isolate the last part using smd_wrap, rah_function, one of the string split plugins or simply a bit of php.

It looks like this in my case:

<txp:php>
global $variable;

$referer = trim($_SERVER['HTTP_REFERER'],"/"); // trim off any trailing slash
$referer_parts = explode('/', $referer);  // break into pieces
$referer_tail = end($referer_parts);  // get last piece
$variable['this_cat'] = $referer_tail;  // make that a variable
</txp:php>

You can then use the variable “this_cat” in your txp template. To avoid other referrers (e.g. links coming from search engines or other sites) producing wrong output, you can then check whether “this_cat” actually matches txp:category1 or txp:category2 and if it does output the appropriate category back-link. That gives you the correct breadcrumb thread, showing the path the visitor took to get to that page.

For a not too complex site, that may be all you need…


TXP Builders – finely-crafted code, design and txp

Offline

#3 2015-01-28 06:48:50

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: Category URL (clean) in breadcrumbs?

Thanks for the explaining! I will experiment with .htaccess.

I use this

txp:site_url /><txp:section />/<txp:category1 /> 

when using one category for article, but if using this in a product catalogue with many cats may not show all of them.

It is a powerful plugin that takes some playing with to get right (due in part to a lack of documentation).

Right. I am still afraid of using it.

Offline

Board footer

Powered by FluxBB