WordPress obtains the number of images in an article (album ).

Source: Internet
Author: User
1. there are two main types of articles in the album category of the website, one is the native album function of WordPress, the other is a common article, and an image is inserted in the article; the requirement in this article is to count the total number of images in the text. For the demonstration effect, see loosky. netarchivescategoryalbum. 2. solution to get Article Images

1. there are two main types of articles in the album category of the website, one is the native album function of WordPress, the other is a common article, and an image is inserted in the article; the requirement in this article is to count the total number of images in the text. For details about the demo, see http://loosky.net/archives/category/album. 2. solution to get Article Images

1. Requirements

There are two main types of articles in the album category of the small site: one is the native album function of WordPress, and the other is a common article with an image inserted in the article; the requirement in this article is to count the total number of images in the text.

For details about the demo, see http://loosky.net/archives/category/album.

2. Solution: function for getting the number of article Images

Put the post_img_number function in functions. php.

Function post_img_number () {global $ post, $ posts; ob_start (); ob_end_clean (); // use do_encode code ($ post-> post_content) to process the photo album statistics $ output = preg_match_all ('// I', do_coding code ($ post-> post_content), $ matches ); $ cnt = count ($ matches [1]); return $ cnt ;}

Note: The biggest improvement of this function is to call the WordPress article content by using do_postal code ($ post-> post_content) instead of using $ post-> post_content, this solution prevents Statistics on images when the native album function is used.

Call a function

The call method is as follows:

 

Original article address: WordPress obtains the number of images in the article (album). Thank you for sharing it with the original author.

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.