The first image of WordPress call article

Source: Internet
Author: User

In the past, when designing a WordPress topic, image calling usually uses attachments in the document. However, in order to save the space of the blog host, most bloggers use external images, in this way, we can't call this method, so we can only use the following method to call the first image of the article,The first image of WordPress call articleThe Code is as follows:

1. Function of the WordPress topic. add the following code to the PHP file. The code is mainly used to find out whether there are any images in the article and call the first image address. The working principle is to find whether the label exists in the article, if there is one, call up the first image. If not, use a designed image instead. This method is very useful as a thumbnail of the article. The specific code is as follows and copied to function. php <? PHP... ?> .

Bytes ----------------------------------------------------------------------------------------------------------------

function catch_that_image() {      global $post, $posts;      $first_img = '';      ob_start();      ob_end_clean();      $output = preg_match_all('//i', $post->post_content, $matches);      $first_img = $matches [1] [0];      if(empty($first_img)){ //Defines a default image        $first_img = "/images/default.jpg";      }      return $first_img;    }

 

Bytes ----------------------------------------------------------------------------------------------------------------

The WordPress topic template calls the catch_that_image () function. The method is simple. Insert "Alt =" <? PHP printf (esc_attr _ ('permalink to % s', 'catchbox'), the_title_attribute ('echo = 0');?> Www.banysky.net helps you with the resource "/>. For example, if I insert a file on the homepage, I modified content. php.

The full text is as follows:

<? PHP/*** the default template for displaying content ** @ package catch themes * @ subpackage catch_box * @ since catch box 1.0 */?> <Article Id = "post-<? PHP the_id ();?> "<? PHP post_class () ;?>> <Header class = "entry-header"> <? PHP if (is_sticky ():?> <Hgroup> <H2 class = "entry-title"> <a href = "<? PHP the_permalink ();?> "Title =" <? PHP printf (esc_attr _ ('permalink to % s', 'catchbox'), the_title_attribute ('echo = 0');?> "Rel =" bookmark "> <? PHP the_title ();?> </A> </H2> <H3 class = "entry-format"> <? PHP _ E ('featured', 'catchbox');?> </H3> 

 

For better display, You need to modify the CSS style to beautify your WordPress theme template.

 

Statement:Calling the first image of the article, if it is an external image, it cannot support the timthumb image cutting function. If you are a WordPress theme designer, we recommend using phpthumb and phpthumb to support external images, don't try it, good luck!

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.