Wordpress pros inside pls: Random Post w/ thumbnails

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonyparra
    Confirmed User
    • Jul 2008
    • 4568

    #1

    Wordpress pros inside pls: Random Post w/ thumbnails

    Ive been trying to add random post with thumbnail to a blog, but cant get it to work, cant find a plugin :

    PHP Code:
    <ul>
    <?php
    $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 0);
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
        <li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><img src="<?php echo 
    
    get_post_meta($post->ID, thumbnail, true); ?>" width="100" height="100">
    </p></li>
    <?php endforeach; ?>
    </ul>
    it spits out only the latest post and repeats it 5 times what the hall am i doing wrong???
    Last edited by tonyparra; 11-05-2011, 02:54 AM.

    High Performance Vps $10 Linode
    Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot
  • tonyparra
    Confirmed User
    • Jul 2008
    • 4568

    #2
    tried this no luck

    Code:
    <li><h3>Some Random posts</h3>
     <ul>
     <?php $posts = get_posts(?orderby=rand&numberposts=5′); foreach($posts as $post) { ?>
     <li><a href=?<?php the_permalink(); ?>? title=?<?php  the_title(); ?>?><?php the_title(); ?></a>
     </li>
     <?php } ?>
     </ul>
     </li>

    High Performance Vps $10 Linode
    Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot

    Comment

    • tonyparra
      Confirmed User
      • Jul 2008
      • 4568

      #3
      Originally posted by tonyparra
      Ive been trying to add random post with thumbnail to a blog, but cant get it to work, cant find a plugin :

      PHP Code:
      <ul>
      <?php
      $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 0);
      $rand_posts = get_posts( $args );
      foreach( $rand_posts as $post ) : ?>
          <li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><img src="<?php echo 
      
      get_post_meta($post->ID, thumbnail, true); ?>" width="100" height="100">
      </p></li>
      <?php endforeach; ?>
      </ul>
      it spits out only the latest post and repeats it 5 times what the hall am i doing wrong???
      this one displays what i want but the permalink is all the same!! i fucking hate you right now wordpress

      High Performance Vps $10 Linode
      Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot

      Comment

      • fris
        Too lazy to set a custom title
        • Aug 2002
        • 55679

        #4
        I did something like this recently, but i picked it via category.
        Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

        Comment

        • tonyparra
          Confirmed User
          • Jul 2008
          • 4568

          #5
          Originally posted by fris
          I did something like this recently, but i picked it via category.
          can you enlighten me please

          High Performance Vps $10 Linode
          Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot

          Comment

          • tonyparra
            Confirmed User
            • Jul 2008
            • 4568

            #6
            Originally posted by tonyparra
            this one displays what i want but the permalink is all the same!! i fucking hate you right now wordpress
            Im sorry i luv you wordpress

            High Performance Vps $10 Linode
            Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot

            Comment

            • Babaganoosh
              ♥♥♥ Likes Hugs ♥♥♥
              • Nov 2001
              • 15841

              #7
              Originally posted by tonyparra
              Im sorry i luv you wordpress
              Wordpress understands we all say things we don't mean when we're angry and it forgives you.
              I like pie.

              Comment

              • bpluva
                Confirmed User
                • Apr 2011
                • 235

                #8
                Originally posted by tonyparra
                this one displays what i want but the permalink is all the same!! i fucking hate you right now wordpress
                Try the code Below after while have_post the_post

                <?php
                global $wpdb;
                $numposts = 32;
                $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT $numposts");
                foreach($rand_posts as $post) :
                setup_postdata($post);
                ?>
                <?php endforeach; ?>

                Comment

                • vdbucks
                  Monger Cash
                  • Jul 2010
                  • 2773

                  #9
                  Here ya go.. the proper way to do it ><

                  For use as the main or only loop:
                  PHP Code:
                  <?php
                  $args1 = array(
                          'posts_per_page'    => 5,
                          'orderby'        => 'rand',
                          'post_status'        => 'publish',
                          'offset'        => 0
                      );
                  query_posts($args1);
                  
                  if (have_posts()) : while (have_posts()) : the_post(); ?>
                      <li>
                      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                      <p><img src="<?php echo get_post_meta($post->ID, thumbnail, true); ?>" width="100" height="100"></p>
                      </li>
                  <?php endwhile; endif; ?>
                  Use this as a secondary loop on a page with multiple loops... will save some resources by using the initial query from the main loop as opposed to creating a new query:
                  PHP Code:
                  <?php 
                  $args2 = array(
                          'posts_per_page'    => 5,
                          'orderby'        => 'rand',
                          'post_status'        => 'publish',
                          'offset'        => 0
                      );
                  $rand_posts = get_posts($args2);
                  
                  foreach ($rand_posts as $post) : setup_postdata($post); ?>
                      <li>
                      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                      <p><img src="<?php echo get_post_meta($post->ID, thumbnail, true); ?>" width="100" height="100"></p>
                      </li>
                  <?php endforeach; ?>

                  Comment

                  • fris
                    Too lazy to set a custom title
                    • Aug 2002
                    • 55679

                    #10
                    Originally posted by tonyparra
                    can you enlighten me please
                    Had someone that wanted to show recent photos from a category, (each photo represents a post *each image thumb taken from posts with attachments*.

                    he always had his galleries in 1 category, so he wanted a widget or a shortcode to show recent pics (galleries)
                    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

                    Comment

                    • tonyparra
                      Confirmed User
                      • Jul 2008
                      • 4568

                      #11
                      Originally posted by vdbucks
                      Here ya go.. the proper way to do it ><
                      Thanks i did get it to work

                      High Performance Vps $10 Linode
                      Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot

                      Comment

                      • vdbucks
                        Monger Cash
                        • Jul 2010
                        • 2773

                        #12
                        Originally posted by tonyparra
                        Thanks i did get it to work
                        If you want to add a category clause to it.. you could add something like the following to the args array...

                        via category id:
                        PHP Code:
                        $args1 = array(
                                'cat'            => 3,
                                'posts_per_page'    => 5,
                                'orderby'        => 'rand',
                                'post_status'        => 'publish',
                                'offset'        => 0
                            );
                        query_posts($args1); 
                        
                        of course, you could also just use the category name like so:
                        PHP Code:
                        $args1 = array(
                                'category_name'        => 'cat_name_here',
                                'posts_per_page'    => 5,
                                'orderby'        => 'rand',
                                'post_status'        => 'publish',
                                'offset'        => 0
                            );
                        query_posts($args1); 
                        
                        or my favorite way, by category slug because I don't like using uppercase letters and/or spaces in my queries:
                        PHP Code:
                        $Obj = get_category_by_slug('cat_slug_here');
                        $catid = $Obj->term_id;
                        $args1 = array(
                                'cat'             => $catid,
                                'posts_per_page'    => 5,
                                'orderby'        => 'rand',
                                'post_status'        => 'publish',
                                'offset'        => 0
                            );
                        query_posts($args1); 
                        

                        Comment

                        Working...