• Hey Guys,

    I have a bit of a problem. I have a random post displayed in my sidebar, along with an image to go with it. Problem is, there are posts from all categories showing up when I have explicitely set it up so posts from only the gallery section should appear.

    Here i first tried:

    <?php $rand_posts = get_posts('numberposts=1&cat=9&orderby=rand');?>

    That didnt work, so I then tried to use:

    <?php $rand_posts = get_posts('numberposts=1&cat=9,-10,-1,-3&orderby=rand');?>

    This also hasn’t made a difference. So posts from all categories are showing up, but I only want posts from the gallery section appearing.

    The site in question: http://www.purekatyperry.com

    Please help or point out where exactly I am going wrong. It will be very much appreciated. Thank you for your time.

    Andrew

Viewing 4 replies - 1 through 4 (of 4 total)
  • first observation: there is not much randomness to the random post: it is always the same post on the frontpage (katy-perry-goes-futuristic-in-german-tv-promo-for-teenage-dream-album-video), and on single.php it is always the same post as the single post.

    you line of code is fine, but what do you so afterwards?
    what is the code after the line that you posted, the code to show the posts?

    did you use ‘setup_postdata()’ in that code?

    http://codex.wordpress.org/Template_Tags/get_posts

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Hey there,

    I think the reason there is little randomness there right now is there is only three blog posts on the site – it was only launched today. I use the same code on another of my sites, and it excludes the said categories correctly and all seems to be perfect with that.

    Here is the exact code im using right now:

    <span class="wpn_heading2">Random Gallery <a class="wpn_toggle-expand wpn_toggle-hide" href="#"></a></span>
    		<div class="wpn_box wpn_expandable" style="text-align:center; padding-top:10px; padding-bottom:5px;">
           <div style="width:260px; margin-left:18px;">
          <?php $rand_posts = get_posts('numberposts=1&cat=9,-10,-1,-3&orderby=rand');?>
     <a href="<?php the_permalink(); ?>"><?php $thumb = get_post_meta($post->ID, "wppt_preset1", true); if($thumb) echo ' <img style="border:3px solid #393939;" src="'.$thumb.'" alt="<?php the_title(); ?>" />'; ?></a>
    
             <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    
        </div></div>
             <div class="wpn_box-bottom"></div>

    there is NO randomness;

    read the link i posted: http://codex.wordpress.org/Template_Tags/get_posts

    the example there shows quite clearly how to use get_posts()

    your code above has none of the ‘foreach’ and ‘setup_postdata()’ that is needed.

    Thread Starter andymacdonalduk

    (@andymacdonalduk)

    Ahh I totally understand now. After modifying my code by using the example at the link you sent me, my random post section now appears to be working perfectly.

    Thanks very much for your advice (and time) Alchymyth!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Random Post Section Showing Content from ALL Categories when Only Should be 1’ is closed to new replies.