Amos Cappellaro
Amos Cappellaro

Reputation: 35

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/ )

Upvotes: 0

Views: 696

Answers (2)

Smuuf
Smuuf

Reputation: 6524

You can use the dirname() function for this.

$url = "http://www.website.com/author-20/article-title";
var_dump(dirname($url));

Output:

http://www.website.com/author-20

Upvotes: 1

Philipp Maurer
Philipp Maurer

Reputation: 2505

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.

Upvotes: 0

Related Questions