Example of extracting thumbnails from a wordpress file

Source: Internet
Author: User

Directly run the code:
1. Add articles with thumbnails
As shown in the following figure:

1) the last one in the topic's functions. php?> Add the following code before:

// Add special thumbnail support

If (function_exists ('add _ theme_support ') add_theme_support ('post-thumbnails ');

    

// Output thumbnail address From wpdaxue.com

Function post_thumbnail_src (){
Global $ post;
If ($ values = get_post_custom_values ("thumb") {// output custom domain Image address
$ Values = get_post_custom_values ("thumb ");
$ Post_thumbnail_src = $ values [0];
} Elseif (has_post_thumbnail () {// if there is a thumbnail, the thumbnail address is output.
$ Thumbnail_src = wp_get_attachment_image_src (get_post_thumbnail_id ($ post-> ID), 'full ');
$ Post_thumbnail_src = $ thumbnail_src [0];
} Else {
$ Post_thumbnail_src = '';
Ob_start ();
Ob_end_clean ();
$ Output = preg_match_all ('//I ', $ post-> post_content, $ matches );
$ Post_thumbnail_src = $ matches [1] [0]; // get the image src
If (empty ($ post_thumbnail_src) {// if no image is in the log, a random image is displayed.
$ Random = mt_rand (1, 10 );
Echo get_bloginfo ('Template _ url ');
Echo '/images/pic/'.w.random.'.jpg ';
// If no image is in the log, the default image is displayed.
// Echo '/images/default_thumb.jpg ';
      }
};
Echo $ post_thumbnail_src;
    }
PS: The above code mainly gets the image link. The order of acquisition is as follows:

Images with a custom field of thumb> featured thumbnails> The first image in the article> random image/default image;

Random Image: make 10 images and place them in the images/pic/directory under the current topic folder. The images are in jpg format and are named after numbers 1-10, for example, 1.jpg; if you do not want to use a random image, remove "//" before the last 5th rows, and then add "//" before the last 7th or 9 rows to log out, add a default image named default_thumb.jpg to the/images/directory of the current topic. In this way, the default image is displayed.

2) add the following code to single. php to display the location of relevant articles:

<H3> Related articles

<Ul class = "related_img">

<? Php
$ Post_num = 4;
$ Exclude_id = $ post-> ID;
$ Posttags = get_the_tags (); $ I = 0;
If ($ posttags ){
$ Tags = ''; foreach ($ posttags as $ tag) $ tags. = $ tag-> term_id .',';
$ Args = array (
'Post _ status' => 'Publish ',
'Tag _ in' => explode (',', $ tags ),
'Post _ not_in '=> explode (', ', $ exclude_id ),
'Caller _ get_posts' => 1,
'Orderby' => 'Comment _ date ',
'Posts _ per_page '=> $ post_num
);

Query_posts ($ args );
While (have_posts () {the_post ();?>
<Li class = "related_box">
<Div class = "r_pic">
<A href = "<? Php the_permalink ();?> "Title =" <? Php the_title ();?> "Target =" _ blank ">
"Alt =" <? Php the_title ();?> "Class =" thumbnail "/>
</A>
</Div>
<Div class = "r_title"> <a href = "<? Php the_permalink ();?> "Title =" <? Php the_title ();?> "Target =" _ blank "rel =" bookmark "> <? Php the_title ();?> </A> </div>
</Li>
<? Php
$ Exclude_id. = ','. $ post-> ID; $ I ++;
} Wp_reset_query ();
    }

If ($ I <$ post_num ){

$ Cats = ''; foreach (get_the_category () as $ cat) $ cats. = $ cat-> cat_ID .',';
$ Args = array (
'Category _ in' => explode (',', $ cats ),
'Post _ not_in '=> explode (', ', $ exclude_id ),
'Caller _ get_posts' => 1,
'Orderby' => 'Comment _ date ',
'Posts _ per_page '=> $ post_num-$ I
);
Query_posts ($ args );
While (have_posts () {the_post ();?>
<Li class = "related_box">
<Div class = "r_pic">
<A href = "<? Php the_permalink ();?> "Title =" <? Php the_title ();?> "Target =" _ blank ">
"Alt =" <? Php the_title ();?> "Class =" thumbnail "/>
</A>
</Div>
<Div class = "r_title"> <a href = "<? Php the_permalink ();?> "Title =" <? Php the_title ();?> "Target =" _ blank "rel =" bookmark "> <? Php the_title ();?> </A> </div>
</Li>
<? Php $ I ++;
} Wp_reset_query ();
    }
If ($ I = 0) echo '<div class = "r_title"> no related articles! </Div> ';
?>
</Ul>
PS: Row 4 $ post_num = 4; indicates that four articles are called. Modify the number as needed.

For more information about css styles, see:

. Related_posts {margin-top: 5px ;}
. Related_img {width: 600px; height: 210px ;}
. Related_box {float: left; overflow: hidden; margin-top: 5px; width: 148px; border-right: 1px # eee solid}
. Related_box: hover {background: # f9f9f9}
. Related_box. r_title {width: auto; height: 72px; font-weight: 400; font-size: 14px; margin: 0 10px; overflow: hidden ;}
. Related_box. r_pic {margin: 6px}
. Related_box. r_pic img {width: 130px; height: 100px; border: 1px solid # e1e1e1; backgrou

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.