1

I need to use the_permalink() in order to construct a simple link from each post . the link should be like

http://www.mysite.com/category/year/post-name?output=something..

I use ?output=something" >

the problem is that the trailing slash of the permalink is giving me an extar trailing slash that is breaking my link .

http://www.mysite.com/category/year/post-name/?output=something.. 

I know that I can construct the structure in another way , but I want to use the permalink (much easier) and i do not want to change the permalink structure sitewide ...

any way of getting rid of the trailing slash ??

  • None of these answers address the fact that a trailing slash is completely valid in this case and should not "break" the link. – Milo Dec 30 '16 at 19:34
2

You can use the trim command to do this, specifically, the rtrim to remove the character from the right side:

$url = rtrim(get_permalink(),'/');

Using a blanket substr -1 technique would cause problems if there isn't a trailing slash in the permalink. Also, the function the_permalink echoes the link, and get_permalink returns it as a string.

1

This post is old but, why not use WordPress build in function untrailingslashit? Added since 2.2.0

function untrailingslashit( $string ) {
    return rtrim( $string, '/\\' );
}
0
echo substr(the_permalink(), 0, -1);
  • +1 to exit .. yep - I answered fast , but I believe that EXIT is right - better rtrim.... – krembo99 Jan 5 '12 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy