I have this URL: www.example.com/dog-to-cat
I have this Title: "Dog-to-Cat"

In the post/page I would like to insert 2 images, dog.jpg and cat.jpg
Also, in the text I would like to use the words "Dog" and "Cat".

Can someone help here - I need to extract first word in URL after "/" (dog) and second word after "-to-" (cat).

2 Answers 2

 function url_fetcher(){
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
    else {

    //echo $pageURL;
    $str = explode('/' ,$pageURL);

    $str1 = $str[3];
    echo $str1.'<br \>';
    $str2 = explode("-", $str1);
    $cat = $str2[0];
    echo $cat.'<br \>';
    $dog = $str2[2];
    echo $dog.'<br \>';


Add the shortcode ['url_fetch'] where you want them. But make sure that your url has the page names seperated by hyphens('-').Specifically cat-to-dog or a-to-b.


I don't think you can do this by using shortcodes, but of course you can create your own shortcode and put in your own custom PHP.

There are three things you want to do:

  1. Grab the URL and split it up into an array
  2. Grab the part of the URL you want from the resulting array
  3. Explode the variable on the - and output it in a or multiple variables, which can then be used in your page.

Of course, this only works on the link you mentioned in your post (.com/name1-to-name2), you can't just randomly throw in some link and expect it to work exactly as with your example. (unless the link is built up 100% the same way)

It's not like a Youtube-link, which is always built up the same way, so you're going to have some work making a functioning shortcode out of this.

Here you can read how to make your own shortcode.

Good luck!

  • well... as I said all URLs are the same
    – Esther Max
    Commented Oct 3, 2014 at 14:08
  • well... as I said all URLs are the same. like this: www.example.com/name1-to-name2 . and the "name1" and "name2" are changing from page to page . but it is the same structure... thank you for your answer but you just sending me to 3 different links and it is just not solving this simple problem.
    – Esther Max
    Commented Oct 3, 2014 at 14:15
  • I'm sending you three different links, so you can create your own function and learn in the process, instead of us creating it for you (although, I see someone did create it for you). You never mention that all URLs are the same btw, you just say "I have this URL"
    – Rvervuurt
    Commented Oct 6, 2014 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.