Search code examples
phpwordpressparentpermalinks

Get parent URL from the_permalink();


So here is my problem. The Wordpress PHP function

the_permalink();

gives me this URL: http://www.website.com/author-20/article-title

I basically need his parent URL. How can I get it? ( http://www.website.com/author-20/ )


Solution

  • If the link is always build that way you can cut out the part after the last /. The way to do that in PHP is with the functions substr and strrpos.

    $parentUrl = substr($permaLink, 0, strrpos($permaLink, '/'));
    

    substr cuts out a part of the string, beginning with the second parameter with the length of the third parameter.

    strrpos searches for the last position of a character in a string.

    If the link structure represents the post parent structure consider this question from the wordpress stackexchange community.