• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Randomising content xenforo 2

Admin

Well-Known Member
Staff member
Administrator
Randomising content can be used in various ways, the most common of which is for header images and logos, or advertising banners.

This guide will explain how it can be done simply with two lines of code (which ironically has taken a hundred lines of explanatory text).

For the purposes of this guide, we'll be adding an image in the Container header advertising position, which will randomly change between 5 different images.


Prepare the images​

The first step is to prepare your images.
I have simply created 5 coloured blocks.

50394133977_0976d6c0a3_o.png



The naming of the images is important.
They must all have the same root name, end with a different integer starting from 1 in numerical order, and have the same file extension, in this example that is .png.

So as you can see, they are all named image and I have then added a separator _, which isn't necessary but it does make the file names easier to read.
Lastly, they all end in a number from 1 to 5. There must be no gaps in these numbers - you can't for example number them 1, 2, 4, 6, 8, if randomising between 5 images.

You can name them however you want, as long as you follow that convention.
Another example would be:
  • banner1.jpg
  • banner2.jpg
  • banner3.jpg
  • banner4.jpg
  • banner5.jpg
Then just upload the images to the server.
I created a new /images directory in /styles/default like so:

50393962281_3460cfcd26_o.png



You can upload them wherever you wish.


Creating the code​

Next we create the code for the random number generation.
This is simply:

HTML:
<xf:set var="$int" value="{{ ($xf.time % 5) + 1 }}" />

There are only two components here which are important.

The first is $int which is the name you are assigning to the variable.
It can be anything you like.

The second is the 5 in ($xf.time % 5).
That value must match the number of images you are going to be rotating between.

The randomness comes from the server time, which is called via $xf.time.

The next line of code will call the images. That's standard HTML with a bit of XF syntax for the relative path and to construct the file name.

HTML:
<img src="{{ base_url(('styles/default/images/image_' . $int . '.png')) }}" />

Breaking that down, you should be able to see that it's:
  • referencing the directory we created earlier - /images
  • with the image file name we saved them as - image_ (without the integer)
  • the variable named above - $int (which provides the integer)
  • and the file extension - .png

The periods . either side of $int perform concatenation, so the resulting image path is styles/default/images/image_$int.png, where $int is a value from 1 to 5.

So in this case, the image path for the first image is styles/default/images/image_1.png.

You can of course expand the code to add a URL, alt text, styling, etc.
For example:

HTML:
<a href="https://www.example.com">
<img src="{{ base_url(('styles/default/images/image_' . $int . '.png')) }}" alt="Header image" width="300" height="150" />
</a>


Adding the code​

The final step is to add the code to the template, which for this guide is the Container header advertising position.

HTML:
<xf:set var="$int" value="{{ ($xf.time % 5) + 1 }}" />

<img src="{{ base_url(('styles/default/images/image_' . $int . '.png')) }}" />

50394134502_3d82b715ea_o.png



That's it!


Now each time the page is refreshed, one of five images will be displayed.

50393962756_de5b36c69c_o.png
 

Facebook Comments

Similar threads
Thread starter Title Forum Replies Date
Admin Yilmaz - Hide Content From Guests vB5 Add-ons 0
Admin Yilmaz - Hide [code,php,html] content from guests (vb5.6.x) Add-ons 0
Admin [MMO] Hide Bb-Code Content System Xenforo 1
cuongpro9x Share plugin Content Protector Pack Wordpress 1
cuongpro9x Share WP Content Crawler - Plugin Leech tuyệt vời cho wordpress Wordpress 2
Admin Jazzaaf Popular Content Xenforo 0
Admin Hướng dẫn đăng ký Google Adsense Content mới nhất tháng 5 năm 2019 - Register Google Adsense Content 2019 simple Tut, tool, mmo 0
Admin Hướng dẫn đăng ký Google Adsense Content mới nhất 2018 - How to register Google Adsense Content 2018 Thảo luận wap việt 0
H Help Lỗi "Extra content at the end of the document" Johncms 0
Admin Prevent thin content to avoid panda penalty Add-ons 0
Admin Hướng dẫn load more content cho mọi trang chủ wordpress Wordpress 0
Admin Share Auto Link Content VBB : Youtube, NCT, IMG, MP3 Zing (HOT) Update 20/10 Add-ons 0
Admin [Mod-Mall] Tynt - Add link to copy/pasted content (for SEO) Add-ons 0
Admin Hide BB Code Content FromUnregistered Users Add-ons 0
Admin Fix [E_WARNING] inet_pton(): Unrecognized address unknown xenforo 2.2.10 Xenforo 0
V Giới hạn dung lượng ảnh tải lên trong Xenforo 2 Xenforo 0
V Hướng dẫn tạo logo giống Xenforo Xenforo 4
V Help Tiện ích thống kê bài viết cho Xenforo 2 Xenforo 0
hungdanchoi5 Forum Xenforo Thảo luận chung 1
Admin Your profile xenforo Xenforo 0
Admin Member Watch xenforo Xenforo 0
@kiss@ xin giúp đỡ về xenforo Hỗ trợ sử dụng diễn đàn 1
Admin Pro Black Xenforo 2 Xenforo 0
olalavui Hướng dẫn Enable User-ID tracking in Google Analytics for Xenforo Xenforo 3
Admin A2soft-pics - (A2)Upload Postimages picture xenforo 2 Xenforo 0
Admin Disable email and password edit xenforo 2 Xenforo 0
Admin Object Storage Adapter xenforo 2 Xenforo 0
cuongpro9x Share Tích hợp toàn bộ sticker xịn của zalo vào xenforo Xenforo 0
Admin Xenforo 2 - Random Ads Code Xenforo 0
Admin Quick Theme Switcher xenforo 2 Xenforo 0
Admin [AP] Member Bars xenforo 2 Xenforo 0
Admin Scroll Progress Xenforo 2 Xenforo 0
Admin Mocha Style xenforo 2 Xenforo 0
Admin German language - translation for XenForo 2.2.1 Xenforo 0
Admin [MMO] Navigation tab icons xenforo 2 Xenforo 0
Admin Hide Sidebar xenforo 2 Xenforo 0
Admin Media Gallery Advertisement Positions Xenforo 2 Xenforo 0
Admin Hướng dẫn xóa sổ hoàn toàn ký tự Nokia S40 cho Xenforo 2 - Remove all nokia s40 characters for xenforo 2 Xenforo 0
Admin Default Style Grey xenforo 1.x Xenforo 0
Admin Thread Prefix Colors xenforo 2 Xenforo 0
Admin Customising the layout with custom user fields xenforo 2 Xenforo 0
tiendiendev Hỏi Dùng xenforo bản bao nhiều là ổn định? Xenforo 2
Admin Remove the name and creation date under the thread title xenforo 2 Xenforo 0
Admin Remove little border on tab menu when scrolling xenforo 2 Xenforo 0
Admin XenForo 2.1.12 Released (Security Fix) Xenforo 0
Admin XenForo 2.2.1 Released (Includes Security Fix) Xenforo 0
Admin XenForo Importers XF2 Xenforo 0
Admin TUOITREIT Add Alt Images Resource Xenforo 2 Xenforo 2.x Premium 0
Admin TUOITREIT Add Alt Text To Images Xenforo 2 Xenforo 2.x Premium 0
Admin Similar threads xenforo 2 Xenforo 0

Similar threads

New posts New threads New resources

Back
Top