~ one ~



How to have random category icons

tips So here's one way to do randomizing category icons. It involves a fair amount of work, but the result is nice :)

Note that I will use square brackets in all the code references below, as Nucleus may otherwise interpret the code as actual commands and you'll never get to see the actual code :)
If you try this trick, just replace the square brackets with angle brackets below.

1) Install NP Random and configure it appropriately.


2) Make a bunch of category icons and save them somewhere. You can be very structured and save them in subfolders named after the categories they are for, or use one folder and put them all in there. Whatever you do, it's a good idea to use the names of the categories somewhere in the folder or filename structure, for the obvious reasons that you will see below.
For the purposes of this example, I'll use a folder structure something like /home/user/images/category1/, category2/, and so forth.

In each of these subfolders, choose one icon you would like to see displayed on the front page, make an extra copy of it, and save that extra copy in the same subfolder, renaming it category1.jpg or category2.jpg etc. (ie. name it with the name of that category). I will explain why this is needed below.

3) Make a bunch of text files listing all the category icons with the appropriate html code for your display.
Example content for one text file:

[img src="images/category1/icon1.jpg" class="caticons" alt="category name"]
[img src="images/category1/icon2.jpg" class="caticons" alt="category name"]


Name the text files also using the category names, for example, category1.txt or humor.txt.

4) In your index and item templates, before your [%body%] tag, add in the following code. You'll need to modify it according to your own stylesheets, category names and icon list files, of course.

[%if(category,catname,category1)%]
[%Random(category1.txt)%]
[%elseif(category,catname,category2)%]
[%Random(category2.txt)%]
[%elseif(category,catname,category3)%]
[%Random(category3.txt)%]
[%else%]
[img src="images/[%category%]/[%category%].jpg" class="leftbox" alt="[%category%]"]
[%endif%]


Basically what you're doing is telling NP Random to display a random line from the appropriate list file for each category.
Since each line in the list file is just an image source tag, the result is a random image display :)

So far, so good, right? Well, there is one catch!

The catch is that the front (home) page doesn't have a category because it's a catchall display that shows all posts from all categories!

To get around this, we have that last line of code that comes in after the "else" statement, and this is why you have to make that extra copy of a caticon in each category - so that something can be displayed on the front page :)

I tried doing this with NP RandomQuote and it didn't work as well. NP RandomImg did not work for me in this particular way either.

The only other workaround replacement for NP Random in this use-case (if you're having trouble with NP Random and it's not working for your version of Nucleus) that I've found to produce a similar result is the image randomizer PHP script I mentioned in my previous post below, and I don't like it quite as much as it seems to create a heavier load on the server for some reason. It loads more slowly, too. NP Random works very quickly so the page loads very fast, the way Nucleus normally does. (Have I mentioned before how fast Nucleus is, and that this is one of the many reasons why I like it so much? :)


tags: , , ,
Category: tips , coding
Posted by: heian
on 03.14.2018 at 19:21:38
618 words

Comments


no comments yet...

Add Comment

Comments are currently disabled for this blog.

This item is closed, it's not possible to add new comments to it or to vote on it

Comments must be approved before being published.

© one.kuroshiro.com (2012-2022) | website by tarasseche solutions | back to top