DESIGNED DEVELOPED DELIVERED

Mob: 07760 392284edward@ee-web.co.uk

ee-web thinks... a bit

Go on, make my day ... leave a comment

Adding a random sort to the category blog in joomla 1.5

October 12th, 2011 by Edward

A client with a joomla wanted to display articles in a random order. Sounds like an opportunity for ee-web design to hack the back of joomla – marvelous. We are working here with joomla 1.5.23 – I shouldn’t think its that different with the later joomlas….

There are two stages to this – set up another option in the drop down menu for the category blog layout and add a random sort to the query.

Adding an option to the category blog
Find root/components/com_content/views/category/tmpl/blog/xml

Look for this code

<param name=”orderby_pri” type=”list” default=”” label=”Category Order” description=”Order items by category”>
<option value=””>No, order by Primary Order only</option>
<option value=”alpha”>Title Alphabetical</option>
<option value=”ralpha”>Title Reverse-Alphabetical</option>
<option value=”order”>Ordering</option>

And add another option thus:

<param name=”orderby_pri” type=”list” default=”” label=”Category Order” description=”Order items by category”>
<option value=””>No, order by Primary Order only</option>
<option value=”alpha”>Title Alphabetical</option>
<option value=”ralpha”>Title Reverse-Alphabetical</option>
<option value=”order”>Ordering</option>
<option vlaue=”random“>Random</option>

Now go to your backend, open the menu item and see if the sort order now provides a “Random” selection in the drop down.

shot of the category blog view

So, half way there – you can now select random order – but it doesn’t do anything useful!

Alter the query
Now open components->com_content->helpers->query.php

and look for this code:

function orderbyPrimary($orderby)
{
switch ($orderby)
{
case ‘alpha’ :
$orderby = ‘cc.title, ‘;
break;

case ‘ralpha’ :
$orderby = ‘cc.title DESC, ‘;
break;

case ‘order’ :
$orderby = ‘cc.ordering, ‘;
break;

which effectively looks to see what selection was made in the back end and then applies that to the sort

We need to add in a random option – like so

function orderbyPrimary($orderby)
{
switch ($orderby)
{
case ‘alpha’ :
$orderby = ‘cc.title, ‘;
break;

case ‘ralpha’ :
$orderby = ‘cc.title DESC, ‘;
break;

case ‘order’ :
$orderby = ‘cc.ordering, ‘;
break;

case ‘random‘ :
$orderby = ‘RAND(), ‘;
break;

default :
$orderby = ”;
break;
}

One last thing – note the word “random” in bold – you can write whatever you like here, just make sure that in the two steps you write EXACTLY the same thing – if you don’t, it wont work!

And, hopefully that’s it – all seemed to work for me.

categories

Edward owns and runs ee-web design and has been building websites for over 12 years. He is passionate about... most things really

Was this post helpful or interesting? Spread the news.

  1. First of all: Thank you for this article. I did the steps an it worked out on my Joomla 1.5 – System.

    You ask your readers to edit “root/components/com_content/views/category/tmpl/blog/xml”.
    Of course you mean “root/components/com_content/views/category/tmpl/blog.xml”.

    If you want to radomize the order of your articles on your frontpage, it is necessary to edit the file “root/components/com_content/views/frontpage/tmpl/default.xml”.

    Comment by happy_programmer — March 22, 2012 @ 5:07 pm

  2. Hi happy_programmer
    thanks for pointing out the typeo – d’oh!

    Glad it was useful for you!

    Edward

    Comment by Edward — March 22, 2012 @ 10:37 pm

  3. Thanks for this – exactly what I needed to show a random subset of the testimonials on a website.
    On J!2.5, instead of editing the core file blog.xml, I created an additional template layout “testimonial blog” by copy blog.xml, blog.php and blog_item.php to testimonial.xml, testimonial.php and testimonial_item.php with suitable edits for my purposes.
    I’d like to be able to do the same with query.php, but I can’t find any hint on how to override a helper with a local template copy.

    Comment by Ken Adam — April 10, 2012 @ 11:08 am

  4. Good point – I haven’t really had a reason to do this in Joomla 2.5. I still find that a lot of my work is on adding functionality to Joomla 1.5 sites for clients who have invested heavily over recent years and aren’t ready to take the re-working that going to J!2.5 would require.

    So, as yet I can’t really throw any light on that issue. If you do work it out – let me know!

    Comment by Edward — April 12, 2012 @ 2:24 pm

  5. Thank you! Finally my site will be randomly ordered http://www.theregoesmypaycheck.com

    Comment by Brian Duke — June 5, 2012 @ 1:39 pm

  6. Great – glad to be of service!

    Comment by Edward — June 11, 2012 @ 8:49 pm

  7. It doesnt work for me:( I see the random option, but the articles are ordered in the same way like before. What should I select on the primary order tab?
    Thanks for your help
    Barna

    Comment by vbarnus — June 27, 2012 @ 7:51 am

  8. You need to set “Category order” to random and “Primary order” to default

    Hope that helps

    Edward

    Comment by Edward — June 27, 2012 @ 4:30 pm

  9. Thanks but doesnt work. Should the order change for every refresh?
    This is the page where shoul order random:
    http://www.gyimesiroland.herobo.com/index.php?option=com_content&view=category&layout=blog&id=34&Itemid=56

    Thanks
    Barna

    Comment by vbarnus — June 28, 2012 @ 9:19 am

  10. Hi Barna – I could see that it doesn’t work on your page. Just recently I did it for the comments page here – and yes, the order changes every time you refresh the page. Are you using Joomla 1.5.x because this only works for that. I could look at the two files for you it you want to post them here or send them over? I have used it on two sites now, so it does work!

    Good luck

    Comment by Edward — June 28, 2012 @ 2:46 pm

  11. Hi there,
    I am having the same problem as vbarnus… The random option is showing but when selected the articles dont show up randomly… The website is running on joomla 1.5.22
    This is the page I want to randomise http://perthshireamber.com/index.php?option=com_content&view=category&layout=blog&id=73&Itemid=279
    Thanks in advance for any help you can give!

    Comment by Julia — June 28, 2012 @ 9:16 pm

  12. Hi Julie – A few weeks back I had a similar problem. In the end it was annoyingly simple. I can’t remember exactly what sorted it but it was playing with the “Primary Order” for the link.

    Make sure the links is “category blog” (obvious I know…)

    check carefully that you have edited the correct functions within the two files (there are some similar looking functions in the others).

    Then make sure the category order is “Random” – expt with the primary order – I find it works for me on default.

    It does work – check http://www.ee-web.co.uk/clients and http://www.coinstreet.org/shopeatdrink/offersandevents.html

    Be happy to look at your files if you want …

    Good luck!

    Comment by Edward — June 28, 2012 @ 10:01 pm

  13. Dear Edward,

    I would send you the files just give me an email please. And I really apreciate you to help me. I just want to test it with this site then if it will work I will do it with another site. http://www.manoprogram.hu But that is a business page so I dont want to test it:)

    Thanks you in advance.
    Barna

    Comment by vbarnus — June 29, 2012 @ 6:46 am

  14. It works but there is a misstype on the code:)
    Ordering
    Random

    value not vlaue:)

    it was 2 days to find the problem and n ot it works with my page. Thanks guys!

    Barna

    Comment by vbarnus — June 29, 2012 @ 1:31 pm

  15. Does anybody know how to make this in the newsflash module? There is a chance to have a random article to show, but is shows only one article, but I need to show all the articles but in random order.
    http://www.manoprogram.hu/szolgaltatasok/arcfestes

    If you have any idea, please let me know.
    Thanks
    Barna

    Comment by vbarnus — July 2, 2012 @ 6:26 am

  16. Sorry about this mis-type! 🙁 What a pain….

    Glad you got it working.

    As to the newsflash module I wouldn’t think its that hard to do, but I have never done it so can’t be sure! A quick search on the joomla forum didn’t really help. I expect you’d have to make a little hack in one of the files for the module (maybe default.php) so that the query returns more than one value.

    Have you posted on the joomla forum?

    Comment by Edward — July 2, 2012 @ 9:23 pm

  17. I found this in the default.php

    I dont know exactly how to configure rand params to show more than 1 item: $flashnum = rand(0, $items -1);

    Do you have any idea?
    Barna

    Comment by vbarnus — July 3, 2012 @ 6:44 am

  18. Hi everyone,

    Ur code works, but only if u have only 1 page in ur category blog. If u have more than 1, the articles are repeated in different pages.

    Any idea to solve this???

    Best Regards

    Jacobo

    Comment by Jacobo — July 7, 2012 @ 10:02 am

  19. OK – well, when I made this I only really wanted one page – I assume you mean that you have pagination on? I didn’t try it for more than one I’m afraid. If I get time I’ll have a look – can you post a url?

    Comment by Edward — July 7, 2012 @ 3:29 pm

  20. Hey,

    Nice job – worked well for me in category blog. I even managed to add this to a section blog following the second part but applying it to file in components/com_content/views/tmpl/blog.xml for the first part.

    The second part remains unchanged.

    Thanks for the pointers – much appreciated.

    Cheers
    Henk

    Comment by Henk — September 5, 2012 @ 2:05 am

  21. Glad it was helpful Henk, and thanks for the extra info!

    Comment by Edward — September 12, 2012 @ 11:40 am

  22. This is a great fix in Joomla 1.5. I know it’s already been mentioned but if you ever do come up with a solution for Joomla 2.5 I’d be extremely grateful. I have a list of accommodation providers on a page which I’d love to display in a random order. Cheers, Mike

    Comment by Mike — September 30, 2012 @ 11:24 am

  23. I know I know! I have recently taken the decision to move over to 2.5 for all my new work (I know I know!) so I expect I’ll come up against this sooner or later and I’ll post back as and when. In the meantime, I can’t believe that it isn’t a basic option within the back-end??? Surely it is.

    Comment by Edward — October 1, 2012 @ 7:54 am

  24. This could not worked for me. here is my Query.php code

    public static function orderbySecondary($orderby, $orderDate = ‘created’)
    {
    $queryDate = self::getQueryDate($orderDate);

    switch ($orderby)
    {
    case ‘date’ :
    $orderby = $queryDate;
    break;

    case ‘rdate’ :
    $orderby = $queryDate . ‘ DESC ‘;
    break;

    case ‘alpha’ :
    $orderby = ‘a.title’;
    break;

    case ‘ralpha’ :
    $orderby = ‘a.title DESC’;
    break;

    case ‘hits’ :
    $orderby = ‘a.hits DESC’;
    break;

    case ‘rhits’ :
    $orderby = ‘a.hits’;
    break;

    case ‘order’ :
    $orderby = ‘a.ordering’;
    break;

    case ‘author’ :
    $orderby = ‘author’;
    break;

    case ‘rauthor’ :
    $orderby = ‘author DESC’;
    break;

    case ‘front’ :
    $orderby = ‘a.featured DESC, fp.ordering, ‘ . $queryDate . ‘ DESC ‘;
    break;

    case ‘random’ :
    $orderby = JFactory::getDbo()->getQuery(true)->Rand();
    break;

    // I have Changed This Code Starts Here

    // case ‘RANDOM’ :
    // $orderby = ‘c.random’;
    // break;

    // Ends Here
    default :
    $orderby = ‘a.ordering’;
    break;
    }

    return $orderby;
    }

    Comment by Tariq Khan — November 2, 2016 @ 7:35 am

  25. thanks for the update Tariq – the fact that this post still gets hits surprises me since I wrote that post some years back!

    Comment by Edward — November 29, 2016 @ 11:23 am

  26. Hi Edward,
    Your blog is amazing and that is the only reason why people going through it still. Expecting more such articles on Joomla updates.
    Thank You.

    Comment by Nimi — July 7, 2017 @ 7:05 am

  27. Hi Nimi – I don’t do a lot in joomla now tbh – I have joined the enemy and mostly work in wordpress. That’s not because I think its better, just because that’s what my clients seem to want.

    I still find it amazing that people comment on these posts that I wrote so long ago!

    Comment by Edward — July 13, 2017 @ 2:51 pm

Leave a comment

*


tag