views:

621

answers:

2

Hello everybody!

My urls for posts in WordPress looks like this: http://localhost:8888/blabla/book/yes-vi-testar

Using the_permalink() would generate "http://localhost:8888/blabla/book/yes-vi-testar" but I want to cut the first 34 characters to get a string like "yes-vi-testar". How do I use php substr in a case like this? I'm confused... I tried

<?php
    $friendlypermalink = substr(the_permalink(), 34);
?>

but that doesnt do the trick.

A: 

As Chacha says, use get_the_permalink(). You can then do something like:

$url = get_the_permalink();
$text = substr($url, strrpos($url, '/') + 1);

//or

preg_match('~[^/]+$~', get_the_permalink(), $m);
$text = $m[0];
Tom Haigh
Wouldn't that return "yes-vi-testar" ?
bisko
@bisko: yes, isn't that what the question is asking how to get though?
Tom Haigh
$url = get_permalink(); did the trick
Fred Bergman
but this is just an attempt to reinvent basename()... and this is not the answer which told about get_permalink()...
Ignas R
@Tom Haigh, sorry, lost my mind for a sec there :) Didn't read the whole question.
bisko
+2  A: 

Use get_the_permalink to get the permalink without echoing it

So

substr(get_the_permalink(), .............);

A lot of of the Wordpress function have 'return' alternates using get as the operative word. IE: get_the_time, get_the_content, etc.

the_title is the only one I believe that doesn't have this option. For the_title you have to pass two empty parameters (the before and after seperators) and either a true or false ... not sure at the moment

the_title("","",true);
Chacha102