• 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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter fert

    (@felixo)

    OK, I managed to get it done using yet another example:

    <div>
    <?php
          $random_query = new WP_Query(array(
          'post__not_in' => $do_not_duplicate,
          'showposts' => 1,
          'cat' => '6',
          'orderby' => 'rand'
          ));
          while ($random_query->have_posts()) : $random_query->the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title();?></a>
        <?php endwhile; ?>
    </div>

    The only thing that’s missing for me, now, is the thumbnail. Just adding postimage(‘thumbnail’) into the linked section does not work because of some directory issue.

    Thank you for this code, I needed the random posts thumbnails displayed as well and I found a solution in using custom fields for those.

    So for each post I have a custom field named “thumbnail” and I put this line:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" alt="" width="XX" height="XX" /></a>

    ..instead of the following one in your code (XX to be replaced with the thumbnail width and height of course):

    <a href="<?php the_permalink() ?>"><?php the_title();?></a>

    thanks

    @dzeta: how do this work automatically?
    perhaps by taking the first picture in the article?
    I’m too lazy to create a custom field for the hundreds of blog posts

    thanks a lot, felixo & DZeta. this is very helpful.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Trying to build a “Random Post & Thumbnail Widget”’ is closed to new replies.