• I have a bunch of categories and sub-categories in hierarchy like this:

    A
    AA
    AB
    AC
    B
    BA
    BB
    C
    CA
    CB etc.

    Right now I display the latest post from any of the A categories AA, AB, AC using this code.

    <?php query_posts('showposts=1&cat=75,76,77'); ?>
      <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>

    What I would like to do is, instead of the latest post, have it choose a random post from those categories. It could display a different post each time the page is loaded, or even better would be to have one post be chosen for the whole day.

    Is there anyway to accomplish this?

    Thanks for reading.

Viewing 9 replies - 1 through 9 (of 9 total)
  • <?php query_posts('orderby=rand&showposts=1&cat=75,76,77'); ?>

    See query_posts()

    Thread Starter amazingprod

    (@amazingprod)

    Thanks

    <?php query_posts('orderby=rand&showposts=1&cat=75,76,77'); ?>
    <?php if (have_posts()) : ?>
     <?php while (have_posts()) : the_post(); ?&gt

    This seems to work. How can I get the post to stick around all day. In other words, whichever post is selected, that is the post for the entire day.

    I’m not aware of a random post thingy like that…

    Thread Starter amazingprod

    (@amazingprod)

    I had a php website once with “Quote of the Day”. There was a text file with a bunch of quotes, one per line. Every day at midnight a job would start up that wrote a random number to a file. Whenever a page was accessed the file was read to see which quote should be used.

    Is there any way to do that with wordpress? Have it read a file or something to get the random number for today and then somehow use that to to pick the post to display.

    Or maybe I could use today’s date to index the into the list of posts or something like that? That way it would change when the date changed, at midnight.

    I am not an expert programmer. I can only make minor modifcations to the php files.

    Thread Starter amazingprod

    (@amazingprod)

    Another thing I would like to be able to do is get the latest post from certain categories, rather than the 10 latest posts from certain categories. In other words, the latest post from each of 10 categories, only one post per category.

    Might look through the repository–here’s a starting point

    https://www.ads-software.com/extend/plugins/search.php?q=random

    Thread Starter amazingprod

    (@amazingprod)

    I think for now I will just go with the random post every time the front page is loaded and see how that works out.

    Maybe it makes the website more interesting to have a different post displayed each time. Kind of like a random jukebox of posts. Here is the site if anyone has any comments or suggestions:

    https://www.lagunabeachbikini.com

    Thread Starter amazingprod

    (@amazingprod)

    Here is something interesting.

    I made the front page display a random post. Everytime the php runs, it loads a different post.

    As a separate issue, I have been trying to speed up my wp site. So I installed W3 Total Cache.

    Well since WPTC caches the front page, the first time it loads the new random page. But after that it gets the static html page which doesn’t change. If I set Maximum Lifetime of Cache Objects to 3600 seconds, I think I will get a new random front page every hour. If I set it to 21600 seconds, every 6 hours. Ot I could make it eery 24 hours.

    So I can get the random page to stick around an arbitrary length of time as a side effect of speeding up the website. Sweet!

    @amazingprod,

    hei, using cache is smart solution. but what happen when we have to show dynamic recent comment in front page. As you know it, this widget will show comments on a post.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How To Get Random Post’ is closed to new replies.