Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-01-05 11:10:18

hablablow
Member
From: Paris
Registered: 2004-12-13
Posts: 309
Website

First image Id when multiple defined

Greetings.
Again I wish you all the best for this new year.

I’m defining several images for an article using Mary’s upm_image plugin.

I’d like to read only the first value of the article image field, if there are many defined.

So we have this array: 12, 3, 6, 89, 65, 89

I want to read, return only the first value: 12

I tried with:

<txp:php>global $article_info; echo $article_image[id]['$image_id'];</txp:php>

Doesn’t work.

Since Mary is not longer active on Txp, I’m asking it here on ‘How do I’.


_
_I plant seeds for future visions. Farmer of your eyes. Subliminal engineer of your minds. eion founder__

Hablablow + Webdesignofficina

Offline

#2 2011-01-05 11:17:26

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,943
Website GitHub

Re: First image Id when multiple defined

Try:

<txp:php>
  global $thisarticle;
  $imgs = explode(',', $thisarticle['article_image']);
  echo $imgs[0];
</txp:php>

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 2011-01-05 11:45:13

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: First image Id when multiple defined

Or, if you’re sure the first item is a number:

<txp:php>
   global $thisarticle;
   echo intval($thisarticle['article_image']);
</txp:php>

Code is topiary

Offline

#4 2011-01-05 13:18:32

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: First image Id when multiple defined

Guillaume, are you using 4.3.0? TXP chooses the first image from the article_image field if there are several defined, so do you need to use PHP code for this?

Offline

#5 2011-01-05 13:54:35

hablablow
Member
From: Paris
Registered: 2004-12-13
Posts: 309
Website

Re: First image Id when multiple defined

Stef, Jeff, Jonathan, thank you folks, I’ll return with the results of this investigation as soon as I have decent results.
Jonathan, no unfortunately, I’m still using 4.2.


_
_I plant seeds for future visions. Farmer of your eyes. Subliminal engineer of your minds. eion founder__

Hablablow + Webdesignofficina

Offline

#6 2011-01-05 17:00:59

hablablow
Member
From: Paris
Registered: 2004-12-13
Posts: 309
Website

Re: First image Id when multiple defined

It outputs nothing. But perhaps should I set the whole context I’m dealing with.

Stef, in fact it’s clashing with one of your plugins smd_gallery and it’s a discussion we had earlier in 2010.

The error I have is: > Invalid argument supplied for foreach() on line 453

The snippet below is fully working except when multiple images are defined, it doesn’t output any image…

And that’s the reason to be of my question. If multiples images are defined, just take the first ID.

There goes my previous code, without adding your suggestions, it’s perfectly working:

<txp:article_custom category='<txp:category />' sort="Posted desc" limit="100" section="section-A">
<txp:variable name="art_img" value='<txp:article_image />' /><txp:if_variable name="art_img" value=""><txp:else />
<txp:if_variable name="thumblist" value="">
<txp:variable name="thumblist"><txp:upm_article_image><txp:upm_img_id /></txp:upm_article_image></txp:variable>
<txp:else />
<txp:variable name="thumblist" value='<txp:variable name="thumblist" />, <txp:upm_article_image><txp:upm_img_id /></txp:upm_article_image>' />
</txp:if_variable>
<txp:php>
   global $article_info;
   $article_info[upm_img_id(array())] = array(
         'id' => article_id(array()),
         'title' => title(array()),
         'section' => section(array()),
      );
</txp:php>
</txp:if_variable>
</txp:article_custom>
<txp:smd_gallery form="thumblist" id="?thumblist" limit="10" pageform="thumbs_pager" wraptag="ul" class="thumbs noscript" navwraptag="div" navbreak="span" navclass="ss-pagination" pagepos="above, below" sort="position,fixed"/>

There goes the new code, by adding your suggestions Stef, it’s throwing the error mentioned earlier :

<txp:article_custom category='<txp:category />' sort="Posted desc" limit="100" section="section-A">
<txp:variable name="art_img" value='<txp:article_image />' /><txp:if_variable name="art_img" value=""><txp:else />
<txp:if_variable name="thumblist" value="">
<txp:variable name="thumblist"><txp:php>global $thisarticle; $imgs = explode(',', $thisarticle['article_image']); echo $imgs[0];</txp:php></txp:variable>
<txp:else />
<txp:variable name="thumblist" value='<txp:variable name="thumblist" />, <txp:php>global $thisarticle; $imgs = explode(',', $thisarticle['article_image']); echo $imgs[0];</txp:php>' />
</txp:if_variable>
<txp:php>
   global $article_info;
   $article_info[upm_img_id(array())] = array(
         'id' => article_id(array()),
         'title' => title(array()),
         'section' => section(array()),
      );
</txp:php>
</txp:if_variable>
</txp:article_custom>
<txp:smd_gallery form="thumblist" id="?thumblist" limit="10" pageform="thumbs_pager" wraptag="ul" class="thumbs noscript" navwraptag="div" navbreak="span" navclass="ss-pagination" pagepos="above, below" sort="position,fixed"/>

Looking at the line in your code Stef and I guess it’s the positon fixed that seems to interfer, but I need it ;)

Thanks.

Edited to add code blocks.

Last edited by jstubbs (2011-01-05 18:57:54)


_
_I plant seeds for future visions. Farmer of your eyes. Subliminal engineer of your minds. eion founder__

Hablablow + Webdesignofficina

Offline

#7 2011-01-05 17:30:56

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: First image Id when multiple defined

The error is most likely because a function that requires an array is getting a string (or nothing) instead.

if_article_image has been available since 4.2.0, so you can eliminate some of your if_variable code.

Surely there’s something similar [edit] simpler than mixing upm_image and smd_gallery?

Sorry, too much code for me to work through to give a better solution.

Last edited by jsoo (2011-01-08 16:16:26)


Code is topiary

Offline

#8 2011-01-08 16:08:20

hablablow
Member
From: Paris
Registered: 2004-12-13
Posts: 309
Website

Re: First image Id when multiple defined

All right… Finally solved it and it was much simpler than I thought by simply asking Mary’s plugin to return only one ID in case of an image list, by using <upm_article_image limit=“1”>.

Here’s the final snippet:

<txp:article_custom category='<txp:category />' sort="Posted desc" limit="100" section="section-A">
<txp:variable name="art_img" value='<txp:article_image />' /><txp:if_variable name="art_img" value=""><txp:else />
<txp:if_variable name="thumblist" value="">
<txp:variable name="thumblist"><txp:upm_article_image limit="1"><txp:upm_img_id /></txp:upm_article_image></txp:variable>
<txp:else />
<txp:variable name="thumblist" value='<txp:variable name="thumblist" />, <txp:upm_article_image limit="1"><txp:upm_img_id /></txp:upm_article_image>' />
</txp:if_variable>
<txp:php>
   global $article_info;
   $article_info[upm_img_id(array())] = array(
         'id' => article_id(array()),
         'title' => title(array()),
         'section' => section(array()),
      );
</txp:php>
</txp:if_variable>
</txp:article_custom>
<txp:smd_gallery form="thumblist" id="?thumblist" limit="10" pageform="thumbs_pager" wraptag="ul" class="thumbs noscript" navwraptag="div" navbreak="span" navclass="ss-pagination" pagepos="above, below" sort="position,fixed"/>

Again thanks for the ideas and suggestions.

Ahmma look at those designers coding… ;)

Last edited by hablablow (2011-01-08 16:10:27)


_
_I plant seeds for future visions. Farmer of your eyes. Subliminal engineer of your minds. eion founder__

Hablablow + Webdesignofficina

Offline

#9 2011-01-08 16:35:12

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: First image Id when multiple defined

hablablow wrote:

Ahmma look at those designers coding… ;)

Highly entertaining ;)

This:

<txp:variable name="art_img" value='<txp:article_image />' /><txp:if_variable name="art_img" value=""><txp:else />
	...
</txp:if_variable>

is identical to this (as long as you are using Txp 4.2.0 or greater):

<txp:if_article_image>
	...
</txp:if_article_image>

It’s not clear to me what the $article_info array is for. I can only guess upm_image uses it somehow. Are you sure it’s needed?


Code is topiary

Offline

#10 2011-01-08 16:41:54

hablablow
Member
From: Paris
Registered: 2004-12-13
Posts: 309
Website

Re: First image Id when multiple defined

LOL @ Highly entertaining !

Well… Fair enough I’m not going to launch a debate about any opposite case… ;)

Thanks Jeff !


_
_I plant seeds for future visions. Farmer of your eyes. Subliminal engineer of your minds. eion founder__

Hablablow + Webdesignofficina

Offline

Board footer

Powered by FluxBB