• Hi I am trying to create 2 functions: One to extract the src from an image in a post and one to get the post slug.

    the goal is to place some code in an excerpt that looks like this:

    <a style="background: url( extracted img src using function ) no-repeat scroll 0 0  transparent;" href=" post slug using function ">
    <span>Read More</span>
    </a>

    I intend to create a template.php file for this code that will be called using the “wp include file” plugin. The functions will be in my functions.php file in my theme folder.

    I found a code snippet that should do the trick for the image src, but I am not sure how to modify it to do what I want it to do. The main problems I am having are:

    How do I get this to target the post it’s on?

    in other words, if this the template is included in the excerpt, then the functions need to target the post belonging to that excerpt. This is meant to be included in multiple post excerpts.

    function get_img_src() {
       $src_content = $youroriginalhtmlwithimage;
       $firstImage = "";
       $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $src_content, $ContentImages);
       $firstImage = $ContentImages[1] [0]; // To grab the first image
       echo $firstImage;
     }

    and how do I get the post_slug?

  • The topic ‘creating a function to extract image src’ is closed to new replies.