Trying to use str_replace to remove a directory from the_permalink() in WordPress:
$the_old_link = the_permalink();
$the_new_link = str_replace('/unwanted_folder', '', $the_old_link);
Trying to use str_replace to remove a directory from the_permalink() in WordPress:
$the_old_link = the_permalink();
$the_new_link = str_replace('/unwanted_folder', '', $the_old_link);
You must be logged in to post a comment.
Instead of
the_permalink()
, you have to useget_permalink()
if you want to store a value in PHP.the_permalink()
is meant for displaying output, not for setting variables.See the following:
http://codex.wordpress.org/Function_Reference/get_permalink
http://codex.wordpress.org/Function_Reference/get_permalink
yes, you need to use
get_permalink()
. This is used to save the permalink in a variableyour codes would see something like this
$the_old_link = get_permalink();
$the_new_link = str_replace( '/unwanted_folder', '', $the_old_link );
then where you want to use the new link just echo it.
an instance can be
<a href="<?php echo $the_new_link; ?>" class"new-link">link</a>