• Resolved markdw

    (@markdw)


    Hi there everyone. I have searched for this and found stuff all around what I am looking for, but nothing on this exact thing, so hopefully someone will be able to help.

    I have a section of my home page that brings in the latest post from a certain category. Only I have got it showing the not post, but instead I have it showing a field from the custom fields. This field is an image.

    So I have an image from the latest posts custom field, which I have called ‘Thumbnail’ in a certain category showing on the home page.

    What I want to do is to randomize this, so that every time a visitor goes to the home page it will show a random custom field image from a post in a specific category.

    Below is the code that I am using to generate latest post from a specific category and show the custom field called Thumbnail. Perhaps this could be adapted slightly to randomize the post shown.

    <?php $recent = new WP_Query("cat=1&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
    			<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_content_limit(200, ""); ?></a><div style="clear:both;"></div>
    			<?php endwhile; ?>

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter markdw

    (@markdw)

    Sorry I pasted the wrong piece of code (that was the div above!) Here it is:

    <?php $recent = new WP_Query("cat=1&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
    			<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "Thumbnail", true); ?>" alt="<?php echo get_post_meta($post->ID, "Theme Name", true); ?> thumbnail" /></a>
    			<?php endwhile; ?>
    nickohrn

    (@nickohrn)

    Hey there. You should be able to do an orderby rand to get what you want.

    <?php $recent = new WP_Query("cat=1&showposts=1&orderby=rand"); while($recent->have_posts()) : $recent->the_post();?>
    			<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "Thumbnail", true); ?>" alt="<?php echo get_post_meta($post->ID, "Theme Name", true); ?> thumbnail" /></a>
    			<?php endwhile; ?>

    Let me know if that worked.

    Thread Starter markdw

    (@markdw)

    Thanks for that but it doesn’t seem to work for me. I am not getting any errors but it still just displays the most recent post from that category.

    Any ideas?

    nickohrn

    (@nickohrn)

    I’m sorry, I thought that would work. I got the information for that from this page: http://codex.wordpress.org/Template_Tags/query_posts#Orderby_Parameters

    Maybe you could mess around with some more of the parameters there. Also, I know this is probably very obvious, but did you make sure you have more than one post in that category already? I’ve made that mistake when trying to get random stuff on other sites before.

    If you do find a solution, be sure to post it here.

    Thread Starter markdw

    (@markdw)

    Yes definitely more than one post in the category. Don’t worry as I have made these simple errors before.

    I will have to have a play and maybe change the way that the database is queried?

    Thread Starter markdw

    (@markdw)

    Finally figured it out. I am using the following code and it is working:

    <?php $rand_posts = get_posts('cat=3&numberposts=1&orderby=RAND()'); foreach( $rand_posts as $post ) : ?>
    
    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "homeimage", true); ?>" alt="<?php echo get_post_meta($post->ID, "Theme Name", true); ?> homeimage" /></a>
    
    <?php endforeach; ?>

    So just to clarify this brings in the random post form category number 3. It shows 1 post and only shows the custom field names ‘homeimage’ which is an image. This image then links to the post itself.

    Hope this helps others.

    nickohrn

    (@nickohrn)

    Thanks for posting back here. I was attempting to do much the same you were doing here on a website I just completed development for. The solution I pointed out worked for me, and I’m surprised it didn’t work for you, too. But thanks for posting your solution!

    nickhorn and markdw, thanks for the “rand” tip. It’s exactly what my client is asking me to do. I needed to randomly display the image posts of a particular category. Image posts and where they will link are defined using custom fields.

    Let me share to everyone as well the exact code I used. Thanks!

    <?php $recent = new WP_Query("cat=34&showposts=3&orderby=rand"); while($recent->have_posts()) : $recent->the_post();?>
    
    	<div class="ad_box">
    		<a href="<?php $values = get_post_custom_values("bigad link"); echo $values[0]; ?>" rel="bookmark"><img src="<?php bloginfo('stylesheet_directory'); ?>/bigads/<?php $values = get_post_custom_values("bigad image"); echo $values[0]; ?>" alt="" /></a>
    	</div>

    This is just what i needed but the problem i have is not all of my post will have the specific custum field.

    Can i miss out the post that dont have this custom field and use the ones that do.

    Thanks!

    Just jumping in on markdw’s post: didn’t work for me but when I swapped out orderby=RAND() for orderby=rand it worked perfectly. Cheers to this thread!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Show Random Posts Custom Fields’ is closed to new replies.