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:
- <? php if ( get_post_meta( $post ->ID, 'thumbnail', true) ) : ?>
- <? php $image = get_post_meta( $post ->ID, 'thumbnail', true); ?>
- <a href= "<?php the_permalink() ?> " rel= "bookmark" title= "<?php the_title(); ?> " ><img src= "<?php echo $image; ?> " alt= "<?php the_title(); ?> " /></a>
- <? php else : ?>
- <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>
- <? 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:
- <? php if ( get_post_meta( $post ->ID, 'thumbnail', true) ) : ?>
- <? php $image = get_post_meta( $post ->ID, 'thumbnail', true); ?>
- <a href= "<?php the_permalink() ?> " rel= "bookmark" title= "<?php the_title(); ?> " ><img src= "<?php echo $image; ?> " alt= "<?php the_title(); ?> " /></a>
- <? php else : ?>
- <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>
- <? php endif ; ?>
Description of the modified code:
- /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!