I currently have an ID of a page that I want to use its permalink as the front of a permastruct of a CPT I’m setting up.

Now I can use get_permalink() but that returns the full URL:

http://www.example.com/imapage/subpage/subsubpage

but all I want to return is imapage/subpage/subsubpage

Is there a function that can do this or do I have to device something that can subtract the non-needed part of the url?

5 s
5

There’s nothing built in to return the bit you want but it should be as easy as using the home_url() function and removing it’s output from the full url eg:

function get_relative_permalink( $url ) {
    return str_replace( home_url(), "", $url );
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *