Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-15 04:44:06

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 304
Website

How do I display the parent category of the current category?

<txp:category /> 

displays the current category. I would like to display the parent of the current category.

I checked textbook, the forum and the plugins, but can’t seem to find anything that does this. Any suggestions?


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#2 2008-01-15 13:10:53

thebombsite
Archived Plugin Author
From: Exmouth, England
Registered: 2004-08-24
Posts: 3,251
Website

Re: How do I display the parent category of the current category?

I’m not sure if this or this plug-in might help.


Stuart

In a Time of Universal Deceit
Telling the Truth is Revolutionary.

Offline

#3 2008-01-15 13:27:00

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 304
Website

Re: How do I display the parent category of the current category?

Thanks a lot, Stuart! Good to hear from you again.

I actually tried ats_master_category_list and it didn’t work very well on my installation (v 4.0.5). The plugin dates from 2004, so that may be the reason.

stw_category_tree is installed on my site. I have the gut feeling that this plugin’s adjunct function stw_get_parent should be able to solve my problem, but I haven’t been able to make it work yet and couldn’t find any documentation. Any idea how to properly implement this function?


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#4 2008-01-15 16:21:33

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,212
Website

Re: How do I display the parent category of the current category?

yep I’d like to know that too… post to the plugin’s forum, I’d say.

Offline

#5 2008-01-15 16:28:38

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 304
Website

Re: How do I display the parent category of the current category?

You expect the display of the parent category to be an attribute of the category tag. Something like parent=“1”… Unfortunately, that is not the case.

I hope somebody has a solution!


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#6 2008-01-15 16:46:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,283
Website GitHub

Re: How do I display the parent category of the current category?

Kjeld wrote:

You expect the display of the parent category to be an attribute of the category tag. Something like parent=“1”

Yeah, would be useful. If nobody comes up with anything non-pluginy I have code that does pretty much what you’re asking as part of smd_if. I could probably yank it out of there and roll it into a separate tiny little plugin if it’s of any use…

Should be able to give it the ability to say whether you want the top level parent or the one at a specific ‘level’ or one from X levels back “up” from the current global cat/cat1/cat2.

I’ll give it a wee while though before I dive into it, ‘cos there may be a one-liner from Mary or Gocom (can if_different help?) that saves having to have yet another plugin installed.

Last edited by Bloke (2008-01-15 16:46:52)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#7 2008-01-15 16:46:59

thebombsite
Archived Plugin Author
From: Exmouth, England
Registered: 2004-08-24
Posts: 3,251
Website

Re: How do I display the parent category of the current category?

I don’t use the plug-in but I did mess around with it a while back. Have you tried something odd like setting min or max levels to “0”?

Unfortunately, if my memory serves, sub-categories was something that was started way back when Dean was still with us, back in the gamma days, but it has never been pushed forward.

I think it probably needs a bit of PHP to drag it out of the database. :(

Last edited by thebombsite (2008-01-15 16:53:32)


Stuart

In a Time of Universal Deceit
Telling the Truth is Revolutionary.

Offline

#8 2008-01-15 17:14:54

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 304
Website

Re: How do I display the parent category of the current category?

thebombsite wrote:

I don’t use the plug-in but I did mess around with it a while back. Have you tried something odd like setting min or max levels to “0”?

Yes, I tried that. Even tried negative numbers. No go. Even if that worked, there would still be the problem of the tag outputting ul tags and not a straight category.

Bloke wrote:

Yeah, would be useful. If nobody comes up with anything non-pluginy I have code that does pretty much what you’re asking as part of smd_if. I could probably yank it out of there and roll it into a separate tiny little plugin if it’s of any use…

That would be great. Being able to do it without another plugin would of course be best, but I don’t think we have that luxury…

I didn’t know your “smd_if”. Just installed it to see what it does. An amazing plugin! It almost scares me when I think about it plugging away in my database. ;-)


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#9 2008-01-15 18:45:18

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,283
Website GitHub

Re: How do I display the parent category of the current category?

Kjeld wrote:

Being able to do it without another plugin would of course be best, but I don’t think we have that luxury…

It certainly looks that way :-( I’ll give it a while longer; it’s only lunchtime across the pond so someone may have a brainwave yet.

Failing that, what sort of thing are you after? How are you intending to use it? Let’s assume the tag is called “parent” (of course it won’t be because of name clashes, but it keeps it short for now).

So, would something like <txp:parent /> read the current category and output, what, it’s immediate parent by default? Or the one at the very top of the tree of wherever you are in the sub-sub-sub-su…categories? And which category should it use by default; the global ?c= I assume?

Next, what do you want it to output? A raw parent category name so you can style it yourself? A link? With a wraptag option? class? title? It’s starting to sound a lot like the category tag now; if it was a PHP class I could extend it, damn!

And then what:

<txp:parent name=“blah” /> to get the parent of a specific named category?
<txp:parent catnum=“1” /> to get the parent from the current article’s category1 instead of the global category? (not sure if this is possible, might need some extra logic in there).
<txp:parent level=“1” /> to get the parent category at a specific “level” (1=top level; 2=1st subcat; 3=next subcat and so on)?
<txp:parent level=”-1” /> to get the parent category one, two, threee.. levels “up” from were we are now in the tree?

What should it do if you ask it for a level that doesn’t exist? Should there be an option to control whether you want nothing out or the “closest” parent to what you specified?

Lots to think about. If you or mrdale could chime in with your thoughts it’d help narrow the scope down. If it comes down to it, what I may do is just take exactly the same options as txp:category, strip off the special ones, work out which category to return then throw all the remaining options at txp:category for it to return the tag. The best I can do without subclassing it.

I didn’t know your “smd_if”. Just installed it to see what it does. An amazing plugin! It almost scares me when I think about it plugging away in my database. ;-)

Hehehe, no database manipulation at all, don’t worry! It’s all right there on the page; I just grab what’s there and (ab)use it. Glad you like it.

Last edited by Bloke (2008-01-15 18:45:35)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#10 2008-01-15 19:40:02

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 304
Website

Re: How do I display the parent category of the current category?

Wow, you are on the ball, Bloke.

What I need for my project is quite simple: a raw name output of the parent at the very top, of category 1 of the current active sub category. This tag will be used in a page.

Say I have just clicked on subcat 2 (2 levels down from top parent) and now have a list of articles for subcat 2. I would need the raw name output for subcat 2’s top parent on that page.

I use id’s in my body tag, navigation categories and other locations to give each top category a color coded page. I need the subcategories to follow the color coding of their top category, and therefore a tag that I can put in my body tag as follows

<body id="<txp:parent />">

It should output the name of the top parent of the current category without any tags or links.

But I noticed there is interest from other people for a parent tag as well. They may have other needs. In that case, I think you may end up creating something very similar to the current category tag… For example:

tag: <txp:parent />

attributes:
  • catnum=“n” —> n=category number (eg 1 or 2); get the parent from the current article’s category1 or 2
  • leveldown=“n” —> n=level (1=top level; 2=1st subcat; 3=next subcat and so on); get the parent category at a specific level
  • levelup=“n” —> n=level removed from current category (1=1 level up; 2=2 levels up from current and so on)
  • link=“boolean” —> Whether to link to articles from the same category. Available values: 1 (yes) or 0 (no).
  • title=“boolean” —> Whether to output category title, rather than name. Available values: 1 (yes) or 0 (no, output name).
  • section=“section name” —> Only link to articles from the specified section.
  • this_section=“boolean” —> Whether to only link to articles from the section containing the current article. Available values: 1 (yes) or 0 (no).

Perhaps even class and wraptag.

(The very best would be if we could have a parent attribute with catnum, leveldown and levelup in the current category tags…)

Although I don’t need most of these attributes urgently, I would certainly be able to make very good use of it. ;-)


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#11 2008-01-15 23:20:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,283
Website GitHub

Re: How do I display the parent category of the current category?

Alright then, how’s this? Here’s a tentative 0.1 version of smd_parent.

Works exactly like txp:category with two extra options:

  • level for going up and down the tree. Can be any positive integer to grab the category from that particular “level” of the tree. Defaults to 1, i.e. the top level; specify a negative number to “back up” from your current sub-category level.
  • catnum to tell the plugin to use the article’s category1 or category2 instead of the global category. You can always override the catnum or global category by using the name attribute.

See the help for usage/examples and let me know how it goes. Works with article, image, link, etc. (note: using catnum forces type to “article” for obvious reasons and also forces the tag to be used in an article context — you’ll get a warning otherwise).

Subject to it working to your satisfaction I’ll release it as a proper plugin.

Go break it!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#12 2008-01-16 04:31:16

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 304
Website

Re: How do I display the parent category of the current category?

Steff, it works like a charm! Thanks so much. I really appreciate this.

My site (under development) shows color coding for the current category. Now, even if you’re way down in the hierarchy of the category tree it still shows the color of the parent category, so the visitor knows that s/he is still within that parent category.

I am using it with smd_if to give me the parent category if the current category has a parent and the current category if the current category is the top. Let me know if you’d like to see how I implemented this, and I’ll send you a link to the site I am building by e-mail.

If anybody wants the actual tags, html and css, just let me know on this thread and I will post it.

I haven’t played around yet with catnum (I use it in a page and not an article) and level. I will do that soon and let you know if I am able to break anything. ;-)

I think smd_if and smd_parent are perhaps the only two tags that allow you to play with the parent category? Combined they are incredibly powerful.

Last edited by Kjeld (2008-01-16 04:31:39)


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

Board footer

Powered by FluxBB