Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2008-12-10 15:34:26
- abacabb
- New Member
- Registered: 2008-12-10
- Posts: 7
Help filtering upm_article_images by category with wet_for_each_image
Hi guys,
I am well into my first site with TXP, and I am absolutely loving it so far. What a beautiful system. I am however having a problem with selectively displaying multiple article images.
What I ultimately would like to do is assign as many images to an article as I like, and then use a form to call that article and display only each image within a certain image category. I’m hoping this will allow me to use different article images with different forms, so that depending on the page or position an article is pulled into, it will display an image that is suited for that space.
Currently I am trying to call the article images in using upm_article_images and then filter them by category using wet_for_each_image, but the following code keeps returning every image for me instead of just the categorized ones. Can anyone point me in the right direction with this?
<div id="home-slideshow-wrap">
<ul id="home-slideshow">
<txp:article_custom category="Slideshow-Home-1" form="slideshow-slide-home" limit="1" />
<txp:article_custom category="Slideshow-Home-2" form="slideshow-slide-home" limit="1" />
</ul>
</div>
<!-------- Slideshow-slide-home form---------------->
<li>
<txp:upm_article_image form="slideshow-slide-image" />
</li>
<!-------- Slideshow-slide-image form---------------->
<txp:wet_for_each_image category="Slideshow-Home" limit="1">
<a href="<txp:permlink />" title="<txp:upm_img_caption />">
<img src="<txp:upm_img_full_url />" width="<txp:upm_img_full_width />" height="<txp:upm_img_full_height />" alt="<txp:upm_img_alt escape="html" />" />
</a>
<div><span><txp:upm_img_caption /></span></div>
</txp:wet_for_each_image>
Offline
Re: Help filtering upm_article_images by category with wet_for_each_image
abacabb wrote:
selectively displaying multiple article images.
Hi abacabb, glad you’re enjoying TXP.
I think, though I’m not 100% as I don’t use upm_image much, that in your Slideshow-slide-image
form you’ll have to either:
- remove the wet_for_each_image tag (after all, upm_article_image with a form should automatically iterate over every image found)
- remove the hard-coded category to wet_for_each_image because that’s overriding whatever is being used in upm_article_image. In this case, use the tags-in-tags feature:
<txp:wet_for_each_image category='<txp:upm_image_category />'>
As I read it at the moment, for every article image that upm_article_image finds, wet_for each image is reading every image in the Slideshow-Home
category, thus you have a loop within a loop which may account for your extra images. I could be wrong as I don’t quite get what you’re doing, but one of those solutions should point you in the right direction. If not, shout back and we’ll try harder to figure it out.
EDIT: alternatively, try smd_gallery :-D
Last edited by Bloke (2008-12-10 15:48:33)
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
Offline
#3 2008-12-10 16:07:31
- abacabb
- New Member
- Registered: 2008-12-10
- Posts: 7
Re: Help filtering upm_article_images by category with wet_for_each_image
Bloke,
Thanks for the quick response! I feel like this is getting closer…
To clarify, what I’m trying to accomplish is to use upm_article_image to return images ONLY from a certain category that are assigned to the article.
So, if i have an article with five images assigned to it, and one of those images is in the category “slideshow”, the form will disregard all the other images and only show me the one in that “slideshow” category. Does that make sense?
Thanks for the link to your gallery plugin, it looks incredibly thorough. I think i will definitely be trying it out in the future, though I have my slideshow functionality and code worked out already in this instance, so I’m just looking for a way to call the correct images up.
Thanks again!
Offline
Re: Help filtering upm_article_images by category with wet_for_each_image
abacabb wrote:
if i have an article with five images assigned to it, and one of those images is in the category “slideshow” the form will disregard all the other images and only show me the one in that “slideshow” category. Does that make sense?
OK, say your article_image field has 5 image IDs in it; you have, potentially, up to 5 different categories?
In that case, do you want one particular category to be a “trigger” that causes all other images to be ignored? And is it always the same trigger? i.e. if the plugin found images in flowers
, birds
, slideshow
, landscapes
, and fruit
, the very act of having one of those images belong to the category “slideshow” would ditch all the rest?
If so, ummm, dunno. That’s a tough one because upm_article_image will work through each of the images in turn and render the contents of the form every time so, by the time you reach the trigger, you’ll have already output up to 4 images’ worth of content and you can’t very well “undo” the stuff you’ve already displayed.
I would guess smd_gallery in collate mode would allow you to use smd_if to check the existence of a particular category and take action if any category “contained” a particular value? Apart from that I’m a bit stuck. Anyone else any ideas? Have I missed something obvious?
If you could guarantee that your trigger image was first in the list then you might be able to do it in a similar vein to the way you indicated above, using something akin to the tags-in-tags idea I presented.
Last edited by Bloke (2008-12-10 16:35:13)
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
Offline
#5 2008-12-10 17:04:33
- abacabb
- New Member
- Registered: 2008-12-10
- Posts: 7
Re: Help filtering upm_article_images by category with wet_for_each_image
Yep, you’re right on point with this.
Not to steer the topic off track, but the reason I want to do it this way is because the site contains various promotional spaces that will get filled with content and link back to their respective articles, and I would like for that information to all be centrally managed within the article itself.
For example, I may have one article about a new hotel that will be opening in the city soon. In various places across the site, i want to promote that article with some text or an image that will link back to it, so I will have different forms to display the content in different ways, and I will use category tags on the article to dictate how it will be promoted.
So, say there are 2 promotional spaces on my home page. One is for a little teaser that will have a headline, a little block of text, and an associated image. The other is a slideshow that will display an image and a caption. Both of these, when clicked, will link back to the full article itself.
To tell TXP to display the proper articles in these spaces, I will use category tags on the article itself. So, if the article is categorized with “home page promotion slot”, the code on the home page will look for anything in that category and display the article headline, excerpt, and the article image that is assigned to the “home page promotion” category. Additionally, if I want to promote it through the slideshow as well, I would also assign the article to the “slideshow home” category, in which case the code on the home page will find anything there, and display the article image assigned to the “slideshow-image” category along with its caption.
This way, when an article is written, all of its promotional material can be attached to it and managed from the article tab, and IMO it keeps things a little more organized and clear for the people who will be managing this content.
Offline
Re: Help filtering upm_article_images by category with wet_for_each_image
abacabb wrote:
In various places across the site, i want to promote that article with some text or an image that will link back to it
Interesting, and I know I sound like I’m just pimping my own plugins here, but that’s exactly why I wrote smd_random_banner. It was so you could cross-promote stuff on your sites without fear of the banner about, say, a new hotel appearing on the article page about that new hotel (because it looks amateurish to advertise stuff you’re already reading about).
You may want to check it out because it’s a slightly different approach to the one you are taking but the end result is the same.
Essentially, every image you create (or file — could be a flash animation, or whatever) is about some subject — be it a new hotel or a new product. So when you create that image you designate one of the fields as a link. In the case of the image it could either be the image name, its alt text or its caption (I’d suggest name since the other two are more useful left as they are). So in your name field you put the link to the article that the image advertises (e.g. /articles/crunjo-resort-hotel
).
Repeat that for every thing you want to advertise. You can categorise your adverts using image/file categories; so you might put all hotels in one category and all clubs/restaurants in another; and so on.
Then the fun begins! In your promotional space form, you put a call to smd_random_banner and get it to choose from categories or a bunch of IDs. The category/IDs can be read from a field in an article if you wish (e.g. the article_image field or a custom field). Then you can optionally tell the plugin to filter the resulting list based on a fixed list of words or words that appear in your article. The match_text
, avoid_text
and compare_to
attributes allow you to say things like:
- Go and pick a random image from the ‘hotels, clubs, restaurants, hydros’ categories, but make sure the caption and alt do not contain the words “spa”, “resort”, “hotel” or “concierge”
Since you’ve designed your articles and your advertising naming convention cleverly, these are all words in your ‘keywords’ field for the article about a hotel. Thus you can do this:
<txp:smd_random_banner categories="hotels, clubs, restaurants, hydros" avoid_text="?keywords" compare_to="alt, caption" link_in="name" limit="1" />
Et voila, you get 1 random advert for some other part of your site that is unrelated to the keywords of the current article. And that image advert is a hyperlink to the article to which the advert pertains.
This way, when an article is written, all of its promotional material (or alternatively promotional material that should not be displayed with this article) can be attached to custom fields and managed from the article tab. The smd_random_banner tag then does the hard work of figuring out which content it should show based on the restrictions you give 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
Offline
#7 2008-12-11 16:37:30
- abacabb
- New Member
- Registered: 2008-12-10
- Posts: 7
Re: Help filtering upm_article_images by category with wet_for_each_image
Very nice, Bloke! Thanks for the explanation… This is a solution that could definitely work for me. However (and it’s entirely possible this is totally irrational), I’m still hung up on the idea of avoiding manual linking, and having all the promos be assigned to their places by categories, so at the point that I totally exhaust my search for a method that will allow such a behavior I will definitely be coming back to this plugin.
In revisiting this problem last night and this morning, I guess (and forgive me if I’m being redundant at all!) the simplest way I can categorize what i’m looking for is this:
Does there exist anything that will allow me to display an article image based on the image’s category?
I’m looking for the functionality of <txp:article_custom category="X" />
within a tag somewhat like <txp:article_image category="X" />
Offline
Re: Help filtering upm_article_images by category with wet_for_each_image
Ah… this guy Bloke… always summoning his own monster plugins.
Don’t listen to him.
Offline
#9 2008-12-12 21:43:46
- abacabb
- New Member
- Registered: 2008-12-10
- Posts: 7
Re: Help filtering upm_article_images by category with wet_for_each_image
Bloke wrote:
I would guess smd_gallery in collate mode would allow you to use smd_if to check the existence of a particular category and take action if any category “contained” a particular value?
So sorry to request more hand holding on this one… I’ve got an understanding of the logic here, but I’m not exactly sure how to cycle through the array generated by collate
mode.
If I write
<txp:smd_gallery collate="1" id="?article_image">
<txp:smd_if field="{category}" operator="eq" value="sample">
...put the result here
<txp:smd_if>
<txp:smd_gallery>
even though I intend to get every result with category of sample
returned, the logic doesn’t work that way with the collated list, does it? I know you can refer to the list elements by adding the number at the end of the replacement tags, but my newbie-ness is preventing me from understanding how to target and return the matching elements.
Last edited by abacabb (2008-12-13 00:09:42)
Offline
Re: Help filtering upm_article_images by category with wet_for_each_image
abacabb wrote:
I’m not exactly sure how to cycle through the array generated by
collate
mode.
It depends if you need to. There is a plugin that can help you out there (smd_each: yet another of mine, dang!) but you may not have to go that far. If all you are trying to do is determine if the category contains a particular value then use the contains
operator instead of eq
.
As you found, the collated list is simply a huge string of all things concatenated, with collatedelim
(a comma by default) between each item. Thus an eq
test must match that entire list exactly, which is pretty tough to engineer!
The problem you might find is if your list is something like:
hotels,restaurants,resampled_cuisine,clubs,bars
then the contains
test will still pass, even though ‘sample’ occurs as part of another word in your list. To circumvent this you need to perform a combined smd_if test like this:
<txp:smd_gallery collate="1" id="?article_image">
<txp:smd_if field="{category}" operator="contains::contains::contains" value="sample,::,sample::,sample," logic="or" param_delim="::">
...put the result here
<txp:smd_if>
<txp:smd_gallery>
That is essentially saying “if the {category}
contains sample
as a discrete entity at the start of the list, or the end of the list, or in the middle of the list, then all systems are go. The delimiter has to be modified to something else (:” in this case) because otherwise the comma used between items in the collated output would confuse the plugin.
Does that make any sense?
Last edited by Bloke (2008-12-13 12:01:11)
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
Offline
#11 2008-12-13 16:30:40
- abacabb
- New Member
- Registered: 2008-12-10
- Posts: 7
Re: Help filtering upm_article_images by category with wet_for_each_image
Bloke wrote:
That is essentially saying “if the
{category}
containssample
as a discrete entity at the start of the list, or the end of the list, or in the middle of the list, then all systems are go. The delimiter has to be modified to something else (:” in this case) because otherwise the comma used between items in the collated output would confuse the plugin. Does that make any sense?
Yes, I think that makes sense, but that condition will return the entire collated list, if it detects sample
, right?
Is there any way for it to detect sample
in that list, and then give me ONLY the items that are associated with sample
… i.e. only the images that belong to the sample
category and nothing else?
Offline
Re: Help filtering upm_article_images by category with wet_for_each_image
abacabb wrote:
that condition will return the entire collated list, if it detects
sample
, right?
Yes. smd_if just checks it but won’t alter anything about it.
Is there any way for it to detect
sample
in that list, and then give me ONLY the items that are associated withsample
… i.e. only the images that belong to thesample
category and nothing else?
I getcha. Erm, that’s totally fried me. Collate mode is for collecting everything together and outputting it at once, optionally plucking numbered entries from the list. Naming entries is much harder to do programmatically (so I ran away from it).
All sorts of complex interactions between smd_gallery, smd_if and smd_each are going through my head at the moment and it is perhaps possible with all that lot but you know what, since you’re hard-coding the sample
category and this is such a twisted way of dealing with article images, how about breaking out the most twisted plugin of them all… smd_query? Ditch smd_gallery and smd_random_banner and try this (untested) beast instead:
<txp:smd_if field="?article_image" operator="contains" value="sample">
<txp:smd_query column="*" table="txp_image" where="category='sample'">
... your gallery anchors / images go here - refer to smd_query docs to get at the columns
</txp:smd_query>
</txp:smd_if>
Does that work? smd_query uses a similar {column_name}
sytax to smd_gallery so you should be able to pretty much plug your gallery form in the container.
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
Offline