2 sam2kb Jun 05, 2011 16:47

I don't understand how this will work: won't it simply generate a list of random posts on the page, not in a widget? What I mean is - where does the result appear? Because I don't see anything appear on the page.
See http://www.schoolofpuppetry.com.au/generator.php?getrandomposts=15&redir=no
Thanks
Try to move this code from index.main.php to posts.main.php
The code above generates a list like this
<ul>
<li>Post title 1</li>
<li>Post title 2</li>
</ul>
Once you get it working, on yourblog/index.php?getrandomposts=15&redir=no, create a Free HTML widget with javascript displaying these results and refreshing every few seconds.
<script type="text/javascript">
jQuery.ajax({
url: "yourblog/index.php?getrandomposts=15&redir=no",
cache: false,
success: function(html){
jQuery("#posts_inserted_here").append(html);
}
});
</script>
<div id="posts_inserted_here"></div>
Ok, sorry I should have been clearer: I don't want it refreshing every X seconds. I want the random post to refresh every time someone clicks the 'random post' link.
Anyway, moving the code to posts.main.php gives me an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RAND, post_ID RAND LIMIT 15' at line 2(Errno=1064)
Your query: ItemList2::Query() Step 1: Get ID list
SELECT DISTINCT post_ID
FROM evo_items__item INNER JOIN evo_postcats ON post_ID = postcat_post_ID
INNER JOIN evo_categories ON postcat_cat_ID = cat_ID
WHERE ( cat_blog_ID = 15 )
AND (post_ptyp_ID IN (1))
AND (post_datestart <= '2011-06-07 17:23:40')
AND ( ( ( post_status = 'private'
AND post_creator_user_ID = 1 )
OR post_status IN ('published','protected') ) )
ORDER BY post_order RAND, post_ID R
AND
LIMIT 15
I guess I should have tested the code before posting...
:) That's ok - it didn't cause an error when I put it in index.main.php, so I didn't pick it up til now.
replace
'order' => 'RAND',
with
'orderby' => 'RAND',
You don't have to reload every few seconds, it's totally up to you how to display the list. The main thing is how to get these posts in the first place.
Thanks, that solved the problem. One last question: how do I call a particular blog? I want to make sure the random posts come from one blog only, not whatever blog the code happens to be installed on.
Replace
global $Blog;
$RandomList = new ItemList2( $Blog, NULL, 'now', $limit );
with
$blg = param( 'blg', 'integer', true );
$BlogCache = & get_BlogCache();
$Blg = & $BlogCache->get_by_ID( $blg );
$RandomList = new ItemList2( $Blg, NULL, 'now', $limit );
not tested!
index.php?getrandomposts=15&blg=22&redir=no
I get the following error:
Fatal error: Call to undefined function get_BlogCache() in public_html/skins/2011home/posts.main.php on line 22
And the following code at the very top of your /skin/index.main.php, right after this string
You can get a list of random posts with AJAX request on this address
Of course it would be a lot better to create a plugin for this because otherwise you spend a lot more resources on pulling out unnecessary posts from the database. This is the easiest method to get what you want.