user2477139
user2477139

Reputation: 606

wordpress extract media url

 $path = parse_url($post->guid, PHP_URL_PATH);
      echo "<pre>";
      print_r($path);
       echo "<br>";

here i get

/wp-content/uploads/2014/01/kl-2-256.png

/wp-content/uploads/2014/04/bg-eBook.pdf

here i want to remove /wp-conent/uploads from these paths and extract only year month and image name

i tried with

  $segments = explode('/', rtrim($path, '/'));

but not working properly every time

is there any proper and best solution?

Upvotes: 2

Views: 265

Answers (2)

Use the list() construct to map the three data you need. The code is exploding the path by / and then looks from behind and passes those values to your mapped variables of list.

$path = '/wp-content/uploads/2014/01/kl-2-256.png';
list($year,$month,$image)=array_slice(explode('/',$path),-3,3);

You can then print $year,$month and $image separately.

Upvotes: 1

MrHunter
MrHunter

Reputation: 1900

Would something like this work for you?

$path = parse_url($post->guid, PHP_URL_PATH);
$path = str_replace("wp-content/uploads/", "", $path);

echo $path;

Upvotes: 1

Related Questions