Isolating and customizing first image in Wordpress

2019-09-21 10:22发布

问题:

I have a Wordpress site which uses a feature image on the posts which link to a page with the post's content (images, content, etc)

I separate out the images from the content with

<?php
    preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
    for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
    echo $images[1][$i];
    }
?> 

I want to target the first image of each individual post (it is not the featured image) I want to put

<h1><?php the_title(); ?></h1>

overtop the first image, to make it act as a header for the post, then I can have the other images and text underneath. not sure how to accomplish this

EDIT - clarification

My index.php shows the featured image of each post in a 'thumbnail gallery' style. each picture links to content-single.php where the content and images are displayed for that post (the featured image is NOT on this page). content-single.php is where i pull out the text and the images

I want to be able to put a title overlapping the first image of the post (NOT the featured image on index.php) because I want the first image of the content to act as a header for each single post page

回答1:

Try This.

    $content = apply_filters ("the_content", $post->post_content);

    preg_match_all('/<img[^>]+>/i',$content, $result);

    echo "<pre>";

    print_r($result);

i hope this is useful for you.!



回答2:

Try This.

  preg_match_all('/<img[^>]+>/i',$get_the_content(), $result);
  echo "<pre>";
  print_r($result);

For Frist Image Try this $result[0][0].

It Get All Images.