Code For A Random Blog Post?
April 27, 2008 8:31 AM Subscribe
I have a blog and would like to add a "Random Post" button.
I'm hosted with a company called Blogware which, unlike Blogger and WordPress, seems to have no no way to add a "random post" button.
I'm not a programmer but suspect this should be fairly easy to do. I'd like advice on how to go about this - do I try to do it in JavaScript? PHP? Something else? Whatever's the easiest solution is best for me. (Major bonus points if someone provides the code itself that would do this, either as a text link or a button I can add.)
All posts on my blog have the basic format of: http://blog.jason.hammond.net/blog/_archives/yyyy/m/dd/
...so if I can define the start date of my blog (Feb 28, 2006) and occasionally update the script with the most recent date of a post as an end point, that would be close enough for what I want. (I try to post daily but don't mind if users end up on a blank day or don't have every single recent post I've made in the range.)
I should also mention that I'm not able to get to the "guts" of the site but can add third-party widgets and other bits of code.
To do this, I think I need to:
- define the start date of the range (2006/2/26)
- define the end date (for now, say 2008/4/26) and be able to easily change this monthly or whenever I get a chance
- generate a random date in the format yyyy/m/dd
- append that to the URL: "http://blog.jason.hammond.net/blog/_archives/"
- go to that URL
Thanks very much in advance for your help!
I'm hosted with a company called Blogware which, unlike Blogger and WordPress, seems to have no no way to add a "random post" button.
I'm not a programmer but suspect this should be fairly easy to do. I'd like advice on how to go about this - do I try to do it in JavaScript? PHP? Something else? Whatever's the easiest solution is best for me. (Major bonus points if someone provides the code itself that would do this, either as a text link or a button I can add.)
All posts on my blog have the basic format of: http://blog.jason.hammond.net/blog/_archives/yyyy/m/dd/
...so if I can define the start date of my blog (Feb 28, 2006) and occasionally update the script with the most recent date of a post as an end point, that would be close enough for what I want. (I try to post daily but don't mind if users end up on a blank day or don't have every single recent post I've made in the range.)
I should also mention that I'm not able to get to the "guts" of the site but can add third-party widgets and other bits of code.
To do this, I think I need to:
- define the start date of the range (2006/2/26)
- define the end date (for now, say 2008/4/26) and be able to easily change this monthly or whenever I get a chance
- generate a random date in the format yyyy/m/dd
- append that to the URL: "http://blog.jason.hammond.net/blog/_archives/"
- go to that URL
Thanks very much in advance for your help!
Response by poster: How do I "call" this code? Just embed it in the page or do I have to put some HTML in there somehow?
posted by Jaybo at 12:12 PM on April 27, 2008
posted by Jaybo at 12:12 PM on April 27, 2008
You should put it in your templates, assuming they're also in PHP.
posted by signal at 5:46 PM on April 27, 2008
posted by signal at 5:46 PM on April 27, 2008
Remember that if any data has been outputted,
posted by devlin at 9:49 PM on April 27, 2008
header
will not function. It must be done before any code is outputted or in a separate file, just incase. What I would recommend is placing that code in another file, and referencing it in your code instead of just putting it directly in your templates.posted by devlin at 9:49 PM on April 27, 2008
Yes, you should probably put this in a separate file. Wrap the code I wrote in <> and save it as something like random.php and upload it to your webserver. If your host doesn't support PHP, then you'll probably have to host it somewhere else or do it in JS, if I'm bored I might write that for you, mefi mail me.
posted by bertrandom at 2:11 AM on April 28, 2008
posted by bertrandom at 2:11 AM on April 28, 2008
This thread is closed to new comments.
$startdate = mktime(0,0,0,2,26,2006);
$enddate = mktime(0,0,0,4,26,2008);
$daysbetween = floor(($enddate - $startdate) / (60 * 60 * 24));
$targetdate = $startdate + (rand(0,$daysbetween) * 60 * 60 * 24);
header("Location: http://blog.jason.hammond.net/blog/_archives/" . date("Y/n/j", $targetdate);
exit();
I didn't test it so your mileage may vary. Give it a shot?
posted by bertrandom at 9:38 AM on April 27, 2008