• dan

    (@daniel-berhane)


    Hello,

    Many posts on my blog don’t have links on keywords and phrases because I was too lazy to do that at the time.
    Now, I wantvto fix that.

    Is there a way to link key words using a search-replace method?
    For example, I want to find all words “Africa” and put a link on them.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’d use something like this…

    function my_add_links_function( $content ){
        $search = array (
            'Africa'
        );
    
        $replace = array (
            '<a href="https://africa.com">Africa</a>'
        );
    
        return str_replace ($search, $replace, $content);
    }
    Thread Starter dan

    (@daniel-berhane)

    Thanks for the prompt reply.

    But where do I apply the code?

    You can add it to the functions.php file in your theme, but I’d suggest creating a child theme so that bit of code doesn’t get over-written on your themes next update.

    Thread Starter dan

    (@daniel-berhane)

    Oh. I see.
    Not sure this is a good or practical idea because I will have insert several codes if I want to link other keywords similarly.

    Thanks very much for the help.

    The way that I’ve done it is about the most efficient way possible. ??

    If you want to replace more words, add them to the $search and $replace arrays.

        $search = array (
            'Africa',
            'USA'
        );
    
        $replace = array (
            '<a href="https://africa.com">Africa</a>',
            '<a href="https://usa.com">USA</a>'
        );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How can I search and link words?’ is closed to new replies.