1

Hi I am trying to create 2 functions for a wordpress site: 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 a function ) no-repeat scroll 0 0  transparent;" href=" post slug using a function ">

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.

and how do I get the post_slug?

the snippet I found is below


function get_img_src() {
   $src_content = $youroriginalhtmlwithimage;
   $firstImage = "";
   $output = preg_match_all('//i', $src_content, $ContentImages);
   $firstImage = $ContentImages[1] [0]; // To grab the first image
   echo $firstImage;
 }
greg
  • 164
  • 2
  • 11
  • Could you clarify what you are trying to do here? How are you looping through the posts? Are you parsing an RSS feed from a WordPress site or something? Because if you are doing this in a regular "Loop" then you can easily get the Slug - or actually just get the whole Permalink. SO when you say "place some code in an excerpt" where are you doing this (which theme file)? That will help a lot, Thanks – thaddeusmt Aug 28 '10 at 20:10
  • Also, is the image attached to the post or are you trying to get the first occurrence of the tag in the post body? – Zahymaka Aug 28 '10 at 23:02
  • lets say I create a post called "product A" which has an image of the "item". In the excerpt for "product A" I want to create an anchor tag with a inline style background image using the "item" as the background image. I can easily just write the code, but I am trying to automate this process since I will not be maintaining the site. I am using a modified twenty ten theme. I figured I can get the post slug or url using the_permalink function, but I still need to covert an image from "product A" to a background image in the "product A" excerpt. – greg Aug 30 '10 at 00:19
  • I am trying to get the first occurrence of the tag – greg Aug 30 '10 at 00:19

1 Answers1

0

Haha nice one ! ;) I was doing exactly this once upon a time. Just use this excellent script:

Using regular expressions to extract the first image source from html codes?

Community
  • 1
  • 1
Sliq
  • 15,937
  • 27
  • 110
  • 143