Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Bug?
I have the following snippet in a page
<txp:variable name="otherpubs" value='<txp:article_custom section="projects,about" limit="999" break=","><txp:if_custom_field name="pubs_by_others"><txp:custom_field name="pubs_by_others" /></txp:if_custom_field><txp:if_custom_field name="multi_event_pubs"><txp:custom_field name="multi_event_pubs" /></txp:if_custom_field></txp:article_custom>' />
<txp:images id='<txp:variable name="otherpubs" />' category="participated" sort="alt desc" limit="999" break="">
<txp:if_different>
<div class="line" id="book_<txp:image_info type="id" />">
<figure itemscope itemtype="http://schema.org/ImageObject"><span itemprop="image" class="grid_6"><a href="<txp:site_url /><txp:image_info type="name" />"><txp:image loading="lazy" loading="lazy" /></a></span>
<figcaption itemprop="caption" class="grid_17"><txp:image_info type="caption" escape="" /></figcaption></figure>
<div class="clear"> </div>
</div>
</txp:if_different>
</txp:images>
but some images were not showing up so I set the site on debug which revealed that there is a comma missing for some images. ie 19942143 should be 1994,2143.
[SQL: SELECT * FROM txp_image WHERE (category LIKE 'participated') AND id IN (2143,2342,19942143,1816,1810,2040,1709,16851296,1296,1735,1736,2192,1361,1332,1359,1674,16881247,1247,10881296,1668,1360,1364,1040,934,1690,1700,461,455,364,377,265,258,270258,180,2370,160,1363,158,157) ORDER BY alt desc LIMIT 0, 999 ]
Can anyone spot if there is bug in my construct or if it is a bug in the system?
>Edited to add that if I add a comma after the image id in the custom field, all is working as expected, but… is there a better way?
Last edited by colak (Today 06:06:13)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Bug?
I have a follow up question too:)
In another page I have:
<txp:if_custom_field name="pubs_by_others">
<txp:images id='<txp:custom_field name="pubs_by_others" />' break="" limit="999">
<txp:if_category type="image" category="participated">
<a href="<txp:site_url />publications/#book_<txp:image_info type="id" />"><txp:image loading="lazy" class="grid_24" /></a>
<txp:else />
<a href="<txp:site_url />publications/mentioned/"><txp:image loading="lazy" class="grid_24" /></a>
</txp:if_category>
</txp:images>
</txp:if_custom_field>
where I would expect all images in the participated category to return <a href="<txp:site_url />publications/#book_<txp:image_info type="id" />">, and all others to return <a href="<txp:site_url />publications/mentioned/">.
Instead, all images return <a href="<txp:site_url />publications/#book_<txp:image_info type="id" />">
Again, any pointers would be appreciated.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Bug?
colak wrote #343425:
I have the following snippet in a page […] but some images were not showing up so I set the site on debug which revealed that there is a comma missing for some images. ie 19942143 should be 1994,2143 … Can anyone spot if there is bug in my construct or if it is a bug in the system?
I suspect that happens when you have an article that happens to have entries in both custom fields pubs_by_others and multi_event_pubs. You’ll get 1994 in one and 2143 in the other but no comma between them.
Probably the easiest way is to just add a comma after the first custom_field option, e.g.:
<txp:variable name="otherpubs" trim>
<txp:article_custom section="projects,about" limit="999" break=",">
<txp:if_custom_field name="pubs_by_others"><txp:custom_field name="pubs_by_others" />,</txp:if_custom_field>
<txp:if_custom_field name="multi_event_pubs"><txp:custom_field name="multi_event_pubs" /></txp:if_custom_field>
</txp:article_custom>
</txp:variable>
Because the comma is inside the if_custom_field container, you shouldn’t get ‘empty commas’ in your list.
…
In the rest of your code, you use if_different in the txp:images loop. If you are intending to prevent duplicate pubs being output, I’m not sure it will work as desired because you are sorting your images by the alt tag (maybe you have a good reason for that), so your if_different will be checking for duplicate code output on successive images sorted by alt tag. That code output (e.g. id and caption) will likely vary too, so my guess is if_different will rarely apply here.
If you want to remove duplicates from your list of image ids, maybe it would easier to do that first with a small php snippet. Place the following after you have defined the variable and before your txp:images tag, e.g.:
<txp:php>
global $variable;
$variable['otherpubs'] = implode(',', array_unique(explode(',', $variable['otherpubs'])));
</txp:php>
That basically turns your list into an array, removes any duplicate values in the array (by keeping only unique values) without changing the order and then converts the array back into a comma-separated string.
…
Finally, you might want to wrap your image output in an:
<txp:if_variable name="otherpubs" value="" not>
… your code …
</txp:if_variable>
because should you have no ids in otherpubs, the txp:images tag outputs all your images in your database. I often trip over that one, which is why I mention that.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Bug?
colak wrote #343426:
In another page, I have […] where I would expect all images in the
participatedcategory to return<a href="<txp:site_url />publications/#book_<txp:image_info type="id" />">, and all others to return<a href="<txp:site_url />publications/mentioned/">. Instead, I only get image ids.
I think this has to do with the fact that if_category responds only to certain cases:
- a page url with the category as
?c=category-nameor/category/category-name - being inside a
category_listloop.
In your case, you’ll need to check against the value using txp:image_info type="category". Try it like this:
<txp:if_custom_field name="pubs_by_others">
<txp:images id='<txp:custom_field name="pubs_by_others" />' break="" limit="999">
<txp:evaluate query='"<txp:image_info type="category" />" = "participated"'>
<a href="<txp:site_url />publications/#book_<txp:image_info type="id" />">
<txp:image loading="lazy" class="grid_24" />
</a>
<txp:else />
<a href="<txp:site_url />publications/mentioned/">
<txp:image loading="lazy" class="grid_24" />
</a>
</txp:evaluate>
</txp:images>
</txp:if_custom_field>
The weird looking '"{tag_value}" = "desired-match"' with quotes around the tag and the desired match is, I believe, necessary for the query to match string types against each other correctly. I just tested that on the demo site and it worked.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Bug?
Thanks so much Julian,
Both are working as expected now.
In the rest of your code, you use
if_differentin thetxp:imagesloop. If you are intending to prevent duplicate pubs being output, I’m not sure it will work as desired because you are sorting your images by the alt tag (maybe you have a good reason for that), so your if_different will be checking for duplicate code output on successive images sorted by alt tag. That code output (e.g. id and caption) will likely vary too, so my guess is if_different will rarely apply here.
I’m sorting by year of publication and title which I include in the alt field which contains something like 2023 - Book title. This is because books are added as they are sent to us (in print or pdf formats), which is, sometimes, years after their publication.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Bug?
colak wrote #343433:
Thanks so much Julian,
Both are working as expected now.
Hmmm, not quite:(
The comma in <txp:if_custom_field name="pubs_by_others"><txp:custom_field name="pubs_by_others" /> appears before each publication.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Pages: 1