How to show random text and images with Textpattern
Here’s my problem: I want to show a (linked) random image (and possibly a short description) on a Textpattern website. By random, I mean that the image and its link should be different every time the page loads. After a quick search, here’s what I found for displaying some random content using Textpattern.
Firstly, the smd_random_text plugin allows you to display text from a variety of sources. It’s not only for simple text though and you can use it to show images from a custom field which could be pretty good for article-specific images. This plug-in seems to be a follow-up to the dru_random_text plug-in which, although old, is also worth considering.
In a similar vein, smd_random_banner enables you to display images/banners, either from your Texpattern images, a text file, or custom field. There are a lot of options for you here.
Despite the advantages of the above plug-ins, they did not quite do what I wanted since I like using Textpattern’s forms for my website content and its organisation. For those of you that don’t use Textpattern, a ‘form’ is simply a snippet of (X)HTML that you create and re-use within the Textpattern environment. Clearly, what I put in each form is completely customisable so if I could use a random form, I can vary the random content each time. Perhaps an image link or a book description … or anything I want.
That’s why this blog post about displaying random forms in Textpattern was just what I needed. Using a a simple PHP array and the rand () function within a Textpattern code block gives the degree of customiseability that I need. And by putting the code in Textpattern conditional tags there’s even more flexibility.