0

it is possible to find tag in the content and replace with link?

function replace_content($content){
  foreach(get_the_tags() as $tag) {
    $out .= $tag->name .',';
    $csv_tags .= '"<a href="/' . $tag->slug . '">' . $tag->name . '</a>"';
  }
  $find  = array($out);
  $replace = array($csv_tags);
  $content = str_replace($find, $replace, $content);
  return $content;
}
add_filter('the_content', 'replace_content');
2

I can see that you are constructing an array with only one element, something like this:

array( 'green, yellow, red' )

instead of an array with multiple elements:

array( 'green', 'yellow', 'red' )

Try to replace these lines:

$out .= $tag->name .','; 
$csv_tags .= '"<a href="/' . $tag->slug . '">' . $tag->name . '</a>"';

with

$find[] = $tag->name;
$replace[] = '<a href="/' . $tag->slug . '">' . $tag->name . '</a>';

and remember to declare the arrays at the top:

$find = array();
$replace = array();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.