WordPress gets statistics on the number of images in an article

as far as I knowWordPressThere is no function to count the number of pictures in the article,So if you want to get the total number of all pictures in the article, you can only add by yourselfWordPressCode to achieve。

This feature is not difficult to achieve, Just a few lines of code, There are many similar tutorials in the search on the Internet。

A small function can make the theme function richer,Friends you like can test it yourself。


First add the following code tofunctions.phpFile。

// WordPressGet the number of pictures in the article

if( !function_exists('get_post_images_number') ){  
    function get_post_images_number(){  
        global $post;  
        $content = $post->post_content;    
        preg_match_all('/<img.*?(?: |\t|\r|\n)?src=['"]?(.+?)['"]?(?:(?: |\t|\r|\n)+.*?)?>/sim', $content, $result, PREG_PATTERN_ORDER);    
        return count($result[1]);    

Then add the following code where you need to count the number of pictures in the article

note: Need to be placed in the loop when using。 

<?php echo get_post_images_number().'Pictures' ?>  
Related Article
Manually open WordPress site maintenance mode

In an updater or plug-in,WordPressThe front end will show that it is in maintenance mode,If you want to temporarily enable Read more

WordPress gets the subcategory of the current category

In productionWordPressWhen the theme is often met, how to be inWordPressThe category page displays the subcategories under the current category Read more

The method of forbidding the automatic saving of WordPress articles

Before, Xiao Bian talked about it《solveWordPressarticleidA simple method of discontinuity》Mentioned inWordPress自动保存article的时间间隔的问题,And today we are talking about direct prohibitionWordPressarticle自动保存的方法,The method is Read more

how to automatically add tag links to the article

Today, I'd like to share a WordPress SEO optimization tip, which can realize the automatic addition of keyword tags in Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image