0

I'm pretty sure this is just a PHP question but I'm asking here in case the problem may inside a WordPress function.

I have term meta called 'intro text' which is simply a description of the term. I'm trying to get the description to link to the parent element.

What I'm trying to do is link a term to its parent term IF:

  1. Parent term exists.
  2. Parent name is contained within the child term description. Eg. 'House music' term description is 'Upbeat Electronic music' where 'Electronic music is the parent term name.

In essence, 'Electronic music' would actually be a hyperlink to 'Electronic music'.

Here's the meat of the code:

function replace_parent_name_with_parent_link() {

    ...

    $link = sprintf( '<a href="%s">%s</a>', get_term_link( $parent ), $parent->name );

    return str_replace( $parent->name, $link, $intro_text );

}

So this code is used as a filter in 2 places which are pretty much identical. The issue is that whitespace isn't preserved when used as one of the filters but not the other.

What I want (what I get in one page):

Upbeat <a href="$link">Electronic music</a>.

What I don't want (what I get in the other page). Notice the lack of a space after 'Upbeat':

Upbeat<a href="$link">Electronic music</a>.

Why is this happening? It's doing this when theres a space before and after the parent name. I've repeatedly looked over my code and can't find a reason why this is happening. Please help.

Note: I've tried with different combinations using trim() but no luck.

0
  1. Did you clear cache after making changes with trim()?

  2. Try with: str_replace( trim( $parent->name ), $link, $intro_text );

  3. Are you sure that the term is Upbeat Electronic music and not UpbeatElectronic music? Try with this to be sure:

    str_replace( trim( $parent->name ), ' ' . $link, $intro_text );

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.