Trying to build a “Random Post & Thumbnail Widget”
-
Hi,
using PHP Code Widget, I’m trying to build something that:
– runs in the sidebar
– randomly picks one post from the past
– displays the title and a thumbnail (automatically generated)As I understand from this thread, I need to create a separate loop. Makes sense, so I took this piece of code, which seems reasonable, and turned it into this:
<?php $randomthumbquery = new WP_Query('showposts=999&random=true'); ?> <?php if ($randomthumbquery->have_posts()) : while ($randomthumbquery->have_posts()) : $randomthumbquery->the_post(); ?> <div> <h4><?php $randomthumbquery->the_title(); ?></h4> <p><?php $randomthumbquery->postimage('thumbnail') ?>; ?></p> </div> <?php endwhile; else: ?> <p>Oops, no posts!</p> <?php endif; ?>
As some of you might see, though I learned (and forgot all about) C# a year ago, I have no clue, how exactly PHP works. The code does not work.
Well, before changing this
<h4><?php the_title(); ?></h4> <p><?php postimage('thumbnail')?>;
into this,
<h4><?php $randomthumbquery->the_title(); ?></h4> <p><?php $randomthumbquery->postimage('thumbnail')?>;
at least a title was displayed (but it’s always the one from the main loop, hence my desire to mess around with the code.
What do I need to do, to make this work? Another problem is that no thumbnail will be displayed, which, to me, seems to be a matter of paths messed up. But again: No clue how to do that in PHP.
Any help would be greatly appreciated!
- The topic ‘Trying to build a “Random Post & Thumbnail Widget”’ is closed to new replies.