Let WordPress theme randomly display thumbnails

WordPress forty-three 16.1K Reading mode

My new Hot News theme used the timthumb.php function to automatically adjust the size of thumbnails and generate cache files. Most overseas CMS themes also use this method to generate thumbnails. However, this method can not intercept the image of the outer chain. This fatal weakness finally made me give up using the timthumb.php function. At present, the new version of HotNews pro2.0 I use adopts the more common method of displaying thumbnails, adds custom fields, and controls the size of thumbnails through CSS. Sometimes there are no pictures in the article, and they are too lazy to make thumbnails. Although there is a default placeholder to replace the position of thumbnails, the article is very monotonous when it is too many, and it also loses the role of thumbnails to attract readers' attention. So the idea of randomly displaying default thumbnails comes out. Please see the specific implementation method below.
The general WordPress magazine theme has the thumbnail function, and the general thumbnail function code is as follows:

  1. <? php  if  ( get_post_meta( $post ->ID, 'thumbnail', true) ) : ?>   
  2. <? php  $image  = get_post_meta( $post ->ID, 'thumbnail', true);  ?>   
  3. <a href= "<?php the_permalink() ?> "  rel= "bookmark"  title= "<?php the_title();  ?> " ><img src= "<?php echo $image;  ?> "  alt= "<?php the_title();  ?> " /></a>   
  4. <? php  else : ?>   
  5. <a href= "<?php the_permalink() ?> "  rel= "bookmark"  title= "<?php the_title();  ?> " ><img src= "<?php bloginfo('template_directory');  ?>/ images/img.jpg"  alt= "<?php the_title();  ?> "  /></a>   
  6. <? php  endif ;  ?>   

Just add a rand() function to the default placeholder link location to randomly call multiple pictures in a specific directory. The following is the modified version:

  1. <? php  if  ( get_post_meta( $post ->ID, 'thumbnail', true) ) : ?>   
  2. <? php  $image  = get_post_meta( $post ->ID, 'thumbnail', true);  ?>   
  3. <a href= "<?php the_permalink() ?> "  rel= "bookmark"  title= "<?php the_title();  ?> " ><img src= "<?php echo $image;  ?> "  alt= "<?php the_title();  ?> " /></a>   
  4. <? php  else : ?>   
  5. <a href= "<?php the_permalink() ?> "  rel= "bookmark"  title= "<?php the_title();  ?> " ><img src= "<?php bloginfo('template_directory');  ?>/ images/random/img<?php echo rand(1,5)?>. jpg"  alt= "<?php the_title();  ?> "  /></a>   
  6. <? php  endif ;  ?>   

Description of the modified code:

  1. /images/random/img<? php  echo  rand(1,5)?>. jpg  

The img in front is the name of the image. We can create several new names: img1.jpg, Pictures of img2.jpg, img3.jpg, img4.jpg, img5.jpg can be placed in the/images/random/(the location of the picture can be modified according to the actual situation.) directory, or pictures in other formats. The number 5 is the preset number of random pictures. You can modify this number and increase the number of random pictures accordingly. Add the above code to the appropriate location of the template, and refresh the page to see how it works.
You can refresh Home page of my blog zmingcx.com , the actual effect of random thumbnails is still very good.

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments forty-three    Visitors thirty-three    Author four
    •  underone
      underone two

      Miraculous, it is so simple to realize, so ingenious

      •  Bagua Siji
        Bagua Siji one

        Already done. love it.

          •  Robin
            Robin

            @ Bagua Siji I also saw your effect. It's good. The content is very novel

            •  Haizi
              Haizi one

              @ Bagua Siji What is the file of this custom field in Hot News 2.4?

            •  Bagua Siji
              Bagua Siji one

              This is how to prevent the same thumbnail from appearing in different logs.

              •  aunsen
                aunsen five

                I came here to worship Brother Bird!

                •  Blinux
                  Blinux three

                  Wow, the effect is getting better and better now

                  •  Depressed
                    Depressed one

                    The building owner and all the experts, can you help me to see what's wrong with me? Why is there only one picture on it? I've been working on it all day, but it still hasn't been done well. I don't know what's wrong with me. I don't understand PHP, only css and div
                    Please help me!!!!!! :cry:

                      •  Thin face
                        Thin face zero

                        @ Depressed Hehe, your method is really good. I finally adjusted it after a whole afternoon. Look at my standing. The effect comes out. If you do something else later, you can copy it directly

                      •  Depressed
                        Depressed one

                        I forgot to write the website just now. sorry
                        By the way, my website is http://459029.506.hostcn.cn/
                        The building owner and all the experts, can you help me to see what's wrong with me? Why is there only one picture on it? I've been working on it all day, but it still hasn't been done well. I don't know what's wrong with me. I don't understand PHP, only css and div
                        Please help me!!!!!!

                        •  The branches are singing again
                          The branches are singing again zero

                          Ha ha, how can I think of this method? I feel cheated. I have to sigh, it's so talented

                          •  huzi
                            huzi two

                            What if you want to specify thumbnails?

                            •  ucer
                              ucer one

                              Good thing, thank you very much.

                              •  zoung
                                zoung zero

                                $post->ID,

                                ‘numberposts’ => ‘1’,

                                ‘post_type’ => ‘attachment’,

                                ‘post_mime_type’ => ‘image’,

                                ‘order’ => ‘ASC’,

                                ‘orderby’ => ‘menu_order’)))

                                foreach($images as $image) {

                                $exehill_image = $image->guid;

                                $exehill_thumb = wp_get_attachment_thumb_url( $image->ID);

                                } else {

                                $exehill_thumb = site_url(‘/wp-content/themes/benzo//images/random/img.jpg’);

                                } ?>

                                Why can't I display any pictures??????

                                •  Haizi
                                  Haizi one

                                  Brother Bird, in which file was this function modified. Waiting for your reply online

                                    •  Robin
                                      Robin

                                      @ Haizi I don't know what you want to modify

                                        •  Haizi
                                          Haizi one

                                          @ Robin Modify the number of randomly displayed pictures

                                      •  Post 80 generations
                                        Post 80 generations two

                                        Brother Bird, what I want to know is that the home page displays four random pictures, while the template only displays two

                                        •  JEFF
                                          JEFF one

                                          Brother Bird, please ask me. I want to display 4 articles of a certain category on the home page. Functions. php defines the 150 × 130 thumbnail of the side thumb, but only two article thumbnails with inserted pictures are displayed. The other two articles have no pictures and do not display random pictures. I don't know where the problem is. Please answer it.
                                          Unable to post code, depressed

                                            •  JEFF
                                              JEFF one

                                              @ JEFF After the screenshot, put the map in qq space http://qpic.cn/Y0YFs7MC
                                              http :// qpic.cn/f4bw5qEX

                                            •  Xiaofengzi
                                              Xiaofengzi zero

                                              One thing I want to do is to create a rotation image effect on the home page. Its image source is the image in the headers folder under the default theme of the wp, and then it is displayed on the rotation. I checked on the Internet and said that it is necessary to traverse. Unfortunately, I won't. Robin can help me. Write a function or a file for me. The call location is where the image of the default theme is displayed. thank you
                                              My email wangxiaofengvip@gmail.com

                                              •  Moya
                                                Moya six

                                                Is this or that file modified?

                                                •  What skin care products are good for summer
                                                  What skin care products are good for summer one

                                                  What we need is this effect~~Just imitated the station, which is also with thumbnails. There are too many articles, and we can't add random pictures one by one. It won't appear too monotonous~~GOOD

                                                  •  sinsky
                                                    sinsky two

                                                    。。。 When did you leave a message... Your WP actually remembers me.

                                                    •  Xiaomi
                                                      Xiaomi one

                                                      echo ‘<img src="'.get_bloginfo('template_url').'/ img/random/.jpg” />‘;
                                                      Cannot execute!

                                                      •  Xiaomi
                                                        Xiaomi one

                                                        echo ‘<img src="'.get_bloginfo('template_url').'/ img/random/.jpg” />‘; How can this change be implemented

                                                        •  maxling
                                                          maxling four

                                                          Thanks for sharing with Brother Bird! Add with success!

                                                          •  Xiao Fang
                                                            Xiao Fang zero

                                                            Hello, please tell me why after I set it, the effect is out, but there is an article at the bottom of the picture!

                                                            Please answer the question, thank you!

                                                            I changed the code to this:
                                                            ID, ‘thumbnail’, true) ) : ?>
                                                            ID, ‘thumbnail’, true); ?>
                                                            <a href="” rel=”bookmark” title=””><img src="” alt=””/>

                                                            <a href="” rel=”bookmark” title=””><img src="/images/random/.jpg” alt=”” />

                                                            .jpg

                                                            The picture is displayed randomly, but there is always the name of the picture at the bottom of the picture. How to remove it,

                                                            For example, this is a picture

                                                            Chart Chart Chart Chart
                                                            Chart Chart Chart Chart
                                                            Chart Chart Chart Chart
                                                            2.jpg

                                                            There is always a picture name below the picture. How to modify it? Thank you!

                                                            •  Yaba Business Card
                                                              Yaba Business Card zero

                                                              Brother Bird, I'm WordPress 3.3.1. The theme is: Twenty Ten 1.3
                                                              The default home page will display a picture of the article (not a thumbnail),
                                                              But if you click the category directory or tag, the image of the article will not be displayed (or called)

                                                              I would like to ask: how to make the pictures of articles also appear in the list when clicking the category directory or the tag.

                                                              •  Dorkadians
                                                                Dorkadians zero

                                                                My Code
                                                                yes
                                                                echo ‘<img width=”140″ height=”98″ src=”‘.$ strResult[1][0].\'”  alt=”‘.trim(strip_tags( $post->post_title )).\'”  />’;     e {  echo ‘<img width=”140″ height=”98″ src=”‘.get_bloginfo(‘template_url’).’/img/thumbnail.png” alt=”‘.trim(strip_tags( $post->post_title )).\'” />’;    

                                                                Using your method to modify will cause code confusion

                                                                How can I change it

                                                                •  macxiu
                                                                  macxiu two

                                                                  Which file is modified

                                                                  •  Yang Zai Navigation
                                                                    Yang Zai Navigation zero

                                                                    Hello, blogger, how do you implement the praise and sharing function behind this article, and what plug-ins do you use

                                                                    •  Suvan
                                                                      Suvan one

                                                                      Ask Brother Bird, the content of my article itself has a picture, how can I get the address of other dimensions of this picture?

                                                                      •  Chu Youcai
                                                                        Chu Youcai zero

                                                                        Bird song, theme 4.4, about thumbnails, at present, the first picture of the article is taken as the thumbnail. If there is no picture, it will be displayed randomly, but the current problem is that the external chain picture cannot be taken, or the format is wrong, the specification is wrong, so I want to change it to directly all random. If no thumbnail is specified, which file code is modified? How to modify

                                                                       anonymous

                                                                      Comment

                                                                      Anonymous netizens
                                                                       :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                                                                      Drag the slider to complete validation