Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Nathan Nawbi

    (@nathan-nawbi)

    closing…

    Thread Starter Nathan Nawbi

    (@nathan-nawbi)

    oh wait. i can use $post->post_content instead.
    never mind….

    Thread Starter Nathan Nawbi

    (@nathan-nawbi)

    thank you very much Venugopal!

    Apologies that I didn’t explain more specific.
    The reason why I am trying to find a efficient way is:

    for example, in a content, there will be a text “SOMETHING”,
    and I want to replace it to be “<div>SOMETHING<span>EVERYTHING</span></div>”.

    But each word is not the same. that is why I wanted to make a pattern.

    so the code I wrote for was to explain that:

    function replace_content($content) {
    $replace_pattern = '#text A-01#' , '#text A-02#';
    $replace_pattern = '#text B-01#' , '#text B-02#';
    $replace_pattern = '#text C-01#' , '#text C-02#';
    
    $content = str_replace('$replace_pattern(text01)', '<div>$replace_pattern(text01)<span>$replace_pattern(text02)</span></div>',$content);
    
    return $content;
    }
    add_filter('the_content','replace_content');

    to find “#text A-01#” in the content and replace it to be”<div>#text A-01#<span>#text A-02#</span></div>”

    each text (like “#text A-01#”) has a matching text (like “#text A-02#”) to be replaced with.

    Do you understand what I am trying to say??

    Thank you very much for helping me!

    Thread Starter Nathan Nawbi

    (@nathan-nawbi)

    thanks, Venugopal!
    I already know that code will work…
    but I have so many text to replace, so I wanted to make it more efficient.
    like,

    function replace_content($content) {
    $replace_pattern = '#text A-01#' , '#text A-02#';
    $replace_pattern = '#text B-01#' , '#text B-02#';
    $replace_pattern = '#text C-01#' , '#text C-02#';
    
    $content = str_replace('$replace_pattern(text01)', '<div>$replace_pattern(text01)<span>$replace_pattern(text02)</span></div>',$content);
    
    return $content;
    }
    add_filter('the_content','replace_content');

    Instead of just adding the entire replacing code, I want to add the ‘$replace-pattern’ so it would be easier to manage.
    I don’t even know how or if there’s any way to do this…

    thanks again!

    Thread Starter Nathan Nawbi

    (@nathan-nawbi)

    thank you so much Ncej!!!
    it works exactly as I wanted!

    you’re so cool! ??
    thanks a lot!

Viewing 5 replies - 1 through 5 (of 5 total)