Textpattern Forum

You are not logged in. Register | Login | Help

#1 2012-01-10 03:37:50

photonomad
Member
Registered: 2005-09-10
Posts: 258
Website

txp:images with image id in url instead of page number

I’ve looked around, but can’t figure out how it might be possible (via plugin or otherwise) to form the url for multiple article_image pages with each image’s id or name (instead of the page number)? It still would need to work with next/previous links.

Currently the url for each page/image looks like this:
http://www.domainname.com/section/title/?pg=3

However, I’m envisioning something along the lines of an article-specific image permalink, where each page/image url remains tied to the image, even if the order of the images is changed in the article_image field (or, by any sort order).

example:
http://www.domainname.com/section/title/?img=14

I’m using the following code in my page to call the images:

<txp:article>
<txp:images autodetect="article" limit="1" pageby="limit">
<img src="<txp:image_url />" />
</txp:images>
</txp:article>

Any thoughts? Thanks!

Last edited by photonomad (2012-01-16 17:20:43)

Offline

#2 2012-01-16 17:11:57

photonomad
Member
Registered: 2005-09-10
Posts: 258
Website

Re: txp:images with image id in url instead of page number

Ok, so it looks like a bunch of people have seen my question, but no remarks thus far. Am I way off-base? Am I missing something completely obvious? If this is the case, then I am really sorry!

If this isn’t the case, then maybe I should explain in more detail:

Motivation/Problem:

A) I am using an article as a portfolio/image gallery. I anticipate that more images will be be added to the article periodically in the future and some images may be deleted as well. The order of the image in the gallery will be affected when these changes take place. As the pages are indexed by search engines, it would be nice if the urls would stay constant even when the page order changes.

B) Same issue affects social media linking: If a Facebook Like Button is tied to the url of each image page in a gallery and then the image order changes the urls, the open graph stats and like counts for each image will be lost.

Question:

Would it be possible via .htaccess or maybe a future plugin or something, to run the page numbers for an article through some type of behind the scenes code in order to swap them with the image id#s so that the URLs for image pages in multi-page image lists (i.e. <txp:images limit=“1” pageby=“limit” />) will be more permanent, seo and social media friendly?

If this were to become possible, I suppose there would have to be a way to construct the links to next and previous images in the list… maybe via special nextimg previmg tags…

Last edited by photonomad (2012-01-16 17:23:57)

Offline

#3 2012-01-16 20:51:56

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 1,923
Website

Re: txp:images with image id in url instead of page number

This is a bit of a guess based on what you wrote and the website in your profile where you also have /?pg=5&image=storm-on-the-paja in your url.

You can use adi_gps to capture the image url variable and then do something with it. The txp tags txp:images, txp:image and txp:image_info will also accept the image name as an attribute instead of the id and assuming that all your photos are .jpg images you could use your image url variable to display the image, e.g. something like:

<txp:adi_gps name="image" quiet="1" />
<txp:if_variable name="image" value="">
  <!-- no image specified: show landing page view -->
<txp:else />
  <txp:images name='<txp:variable name="image" />.jpg'>
    <img src="<txp:image_url />" />
  </txp:images>
</txp:if_variable>

You can then leave out the pg variable so you generate an url like /gallery-name/?image=name-of-image. It’s a messy url but that’s okay for google, social media sites etc.

Theoretically you could use an .htaccess RewriteRule to make this a clean url – or alternatively gbp_permanent_links – but you may need to adjust the url scheme you’re using at present.

There are two caveats here:

  • the assumption is that you have jpg images (which is probable in your case)
  • images must have distinct image names.

TXP Builders – finely-crafted code, design and txp

Offline

#4 2012-01-16 22:53:46

photonomad
Member
Registered: 2005-09-10
Posts: 258
Website

Re: txp:images with image id in url instead of page number

Thanks, Jakob! I am going to play around with adi_gps and will report back.

Offline

#5 2012-01-17 15:25:19

photonomad
Member
Registered: 2005-09-10
Posts: 258
Website

Re: txp:images with image id in url instead of page number

My article_image field contains this list: 245,239,246,238,237,251,243,241,244,236,242,250,247,240,248,249

What is the easiest way to figure out that 241 is previous and 236 is next for image id 244?

My brain fails me. I’ve way over thought it and right now (temporary link to test example) I am using all sorts of variables to create the next/previous links that ultimately fail when I get to the first and last image. FYI: The next/prev links appear when mouse hovers over the image.

Painfully, here’s a peak at all the variables I’m using to try and make it work:

Above the head:

<txp:adi_gps name="image" quiet="1" />
<txp:variable name="count" value="0" />
<txp:article>
<txp:smd_if field="urlvar:image" operator="undefined"><txp:else />
<txp:images break="" sort='FIELD(ID,<txp:custom_field name="article_image" />)'>
<txp:adi_calc name="count" add="1" />
<txp:variable name='<txp:image_info type="id" />' value='<txp:variable name="count" />' />
<txp:variable name='<txp:variable name="count" />' value='<txp:image_info type="id" />' />
</txp:images>
<txp:adi_calc name='<txp:variable name="image" />' subtract="1" result="prevseqn" />
<txp:adi_calc name='<txp:variable name="image" />' add="1" result="nextseqn" />
<txp:adi_calc name='<txp:variable name="prevseqn" />' result="previmg" />
<txp:adi_calc name='<txp:variable name="nextseqn" />' result="nextimg" />
</txp:smd_if>
</txp:article>

In the page for the image in and next/prev links (left out superflous divs/styling):

<txp:images id='<txp:variable name="image" />'>
<img src="<txp:image_url />" />

Next image link:
<a class="next thumb-link" href="<txp:site_url /><txp:article_url_title />/?image=<txp:variable name="nextimg" />&name=<txp:mta_sanitize><txp:image_info id='<txp:variable name="nextimg" />' type="alt" /></txp:mta_sanitize>" title="view next image"></a>

Previous image link:
<a class="prev thumb-link" href="<txp:site_url /><txp:article_url_title /><txp:if_variable name="prevseqn" value="0"><txp:else />/?image=<txp:variable name="previmg" />&name=<txp:mta_sanitize><txp:image_info id='<txp:variable name="previmg" />' type="alt" /></txp:mta_sanitize></txp:if_variable>" title="view previous image"></a>

</txp:images>

Last edited by photonomad (2012-01-17 15:58:43)

Offline

#6 2012-01-17 17:37:48

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 1,923
Website

Re: txp:images with image id in url instead of page number

Yes, I was thinking about that too. Will the tactic of using the next number always work if the images are not consecutive?

Another idea albeit entirely untested: Did the previous way you created the image list with txp:images and pg= variable always produce the right links to the next/prev image even if not consecutive? If so, maybe you can use that to create your thumbnails and next/prev image links and then simply rewrite the output it produces to replace the url variable with your own variable name, e.g.:

<a href="<txp:rah_replace from="pg=" to="image="><!-- your link tag here --></txp:rah_replace>">next</a>

(or something along those lines).

EDIT: ah, scratch that. I just realised that the pg= variable doesn’t correspond to the actual image id number.

Last edited by jakob (2012-01-17 17:40:20)


TXP Builders – finely-crafted code, design and txp

Offline

#7 2012-01-17 17:49:06

photonomad
Member
Registered: 2005-09-10
Posts: 258
Website

Re: txp:images with image id in url instead of page number

Thanks for taking a stab, Jakob! This is tough… The code I posted above does work (except for the previous/last images), but it seems so convoluted to use all of those variables within variables, etc.

Would there be a way to find the next/previous values with some raw php and regex?

Offline

#8 2012-01-17 18:04:12

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 1,923
Website

Re: txp:images with image id in url instead of page number

Probably you can do it with some raw php by converting your comma-separated list in your article_image field into an array (a list of items) with php using explode(), then using array_search() to find the position you are in the array based on the value of the image url variable (i.e. the current image). You can then use php’s next() and prev() to return the image id numbers of the previous and next images in the array. Written out in words that looks a lot but it should only require a few lines.
Note, you can store the results as txp variables in php as described in this thread. Further down in your code you can then use the txp:variables you created as normal to create your actual links.

I have to rush now, so I can’t look in any more detail but maybe you’ll find some more pointers here.

Last edited by jakob (2012-01-17 18:06:00)


TXP Builders – finely-crafted code, design and txp

Offline

#9 2012-01-17 20:16:13

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,929
Website

Re: txp:images with image id in url instead of page number

I’m with jakob: ditch the plugins and use some PHP:

<txp:php>
global $variable, $thisarticle;
$imgs = do_list($thisarticle['article_image']);
$currimg = (int) gps('image');
$found = ($currimg) ? array_search($currimg, $imgs) : 0;
$variable['curr_image'] = $found;
$variable['prev_image'] = ($found < 1) ? '' : $found-1;
$variable['next_image'] = ($found >= count($imgs)-1) ? '' : $found+1;
</txp:php>

PREV: <txp:variable name="prev_image" />
CURR: <txp:variable name="curr_image" />
NEXT: <txp:variable name="next_image" />

It’s a 5-minute dirty beast, but it works.

EDIT: Added (int) cast as per Gocom’s recommendation below.

Last edited by Bloke (2012-01-18 09:17:25)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#10 2012-01-17 20:19:06

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 1,923
Website

Re: txp:images with image id in url instead of page number

Cool! and using txp’s internal functions too: much better.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB