Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-09-03 15:37:29

qrayg
Member
From: USA
Registered: 2004-08-27
Posts: 81
Website

Category List Question

I’m trying to display an image on a category landing page, but only if the category is the child of a specific parent category. (I hope that makes sense.)

I tried the following:

<txp:category_list parent="platform" categories='<txp:category />'>
<p class="img"><img src="//placehold.it/300x300/333/444&amp;text=<txp:category />" class="img-circle" /></p>
</txp:category_list>

As well as:

<txp:category_list categories='<txp:category />' exclude='<txp:category_list parent="Genre"><txp:category />,</txp:category_list>'>
<p class="img"><img src="//placehold.it/300x300/333/444&amp;text=<txp:category />" class="img-circle" /></p>
</txp:category_list>

I’m trying to spit out the category name ONLY if the category is a child of “platform”.

The problem is that it is also displaying the name/image even if the category is not a child of “platform”. I’m assuming that the “categories” attribute overrides the “parent” and “exclude” attributes.

Is there a better way to achieve what I’m looking for?

Offline

#2 2014-09-03 16:37:26

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Category List Question

qrayg wrote #283454:

Is there a better way to achieve what I’m looking for?

I think how do I detect I am on a child category…? should give you what you are looking for.

Edit: This seemed to work when I tested it.

<txp:category_list parent="platform">
  <txp:if_category name='<txp:category />'>
    <txp:variable name="done" value="1" />
  </txp:if_category>
</txp:category_list>

<txp:if_variable name="done" value="1">

<txp:category_list parent="platform" categories='<txp:category />'>
<p class="img"><img src="//placehold.it/300x300/333/444&amp;text=<txp:category />" class="img-circle" /></p>
</txp:category_list>

</txp:if_variable>

Last edited by michaelkpate (2014-09-03 16:42:05)

Offline

#3 2014-09-03 16:43:23

qrayg
Member
From: USA
Registered: 2004-08-27
Posts: 81
Website

Re: Category List Question

Awesome!. Thank you so much.

Offline

#4 2014-09-03 16:56:13

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Category List Question

Hey Michael,

That was indeed a cool piece of code. Did you consider submitting it to txptips?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#5 2014-09-03 17:06:43

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Category List Question

colak wrote #283458:

Hey Michael,

That was indeed a cool piece of code. Did you consider submitting it to txptips?

Did you click through to the old topic I linked to? I really just stole it verbatim from what etc whipped up for the original questioner (who I think you are sort of familiar with). But it probably is something worth submitting to txptips so people can find it easier.

Offline

#6 2014-09-03 17:11:40

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Category List Question

michaelkpate wrote #283460:

Did you click through to the old topic I linked to? I really just stole it verbatim from what etc whipped up for the original questioner (who I think you are sort of familiar with). But it probably is something worth submitting to txptips so people can find it easier.

Hey that was one of my questions!!! Alzheimer’s rules here:)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#7 2014-09-03 17:25:37

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Category List Question

colak wrote #283461:

Hey that was one of my questions!!! Alzheimer’s rules here:)

I was working on a template the other day trying to figure out a way to only add a comma when there was a second category and ended up stumbling across How to separate categories with a comma only when appropriate and having no memory of that discussion at all.

So it happens to all of us.

Offline

#8 2014-09-03 20:35:41

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Category List Question

To be honest, if I had etc_query (or smd_query) installed, I would certainly prefer

<txp:if_category>
<txp:etc_query globals="pretext"
	data="SELECT 1 FROM txp_category WHERE name='{?c||doSlash}' AND type='article' AND parent='platform'"
>
	<p class="img"><img src="//placehold.it/300x300/333/444&amp;text=<txp:category />" class="img-circle" /></p>
</txp:etc_query>
</txp:if_category>

to the core solution, it’s more straightforward.

On the other hand, I don’t think you need the second category_list above:

<txp:category_list parent="platform">
	<txp:if_category name='<txp:category />'>
		<txp:variable name="match" value="1" />
	</txp:if_category>
</txp:category_list>

<txp:if_variable name="match" value="1">
	<p class="img"><img src="//placehold.it/300x300/333/444&amp;text=<txp:category />" class="img-circle" /></p>
</txp:if_variable>

Edit: and one should wrap all this in <txp:if_category />.

Last edited by etc (2014-09-03 20:45:05)

Offline

#9 2014-09-04 03:29:23

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Category List Question

Of course, if you look at the code, setting the variable to boolean true doesn’t actually do anything useful in this case.

<txp:category_list parent="platform">
  <txp:if_category name='<txp:category />'>
    <p class="img"><img src="//placehold.it/300x300/333/444&amp;text=<txp:category />" class="img-circle" /></p>
  </txp:if_category>
</txp:category_list>

Offline

Board footer

Powered by FluxBB