Horje
How to Extract Auto the First Image from the Post Content

This code will automatically extract the first image associated with a post and allow you to display/use it by calling the getImage function.


Methods-1: Auto Extract the First Image from the Post Content

Just copy and paste following codes to yourdomain.com/wp-content/themes/activated-theme/fucntions.php in the most below.

Example: PHP
// AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST
function getImage($num) {
    global $more;
    $more = 1;
    $link = get_permalink();
    $content = get_the_content();
    $count = substr_count($content, '<img');
    $start = 0;

    for($i=1;$i<=$count;$i++) {
        $imgBeg = strpos($content, '<img', $start);
        $post = substr($content, $imgBeg);
        $imgEnd = strpos($post, '>');
        $postOutput = substr($post, 0, $imgEnd+1);
        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
        $image[$i] = $postOutput;
        $start=$imgEnd+1;
    }

    if(stristr($image[$num],'<img')) {
        echo '<a href="'.$link.'">'.$image[$num]."</a>";
    }
    $more = 0;
}





Related Articles
How to Resize Sharpen Images (only JPEG) Wordpress Image Shortcode
How to Extract Auto the First Image from the Post Content Wordpress Image Shortcode

Single Articles
Methods-1: Auto Extract the First Image from the Post ContentWordpress Image Shortcode

Read Full:
Wordpress Image Shortcode
Category:
Web Tutorial
Sub Category:
Wordpress Image Shortcode
Uploaded:
1 year ago
Uploaded by:
Admin
Views:
68



Share on: