• Resolved Nathan Nawbi

    (@nathan-nawbi)


    Hi. I need some help! I want to do something like this:
    1) find same words, which are set as tag, in post contents
    2) wrap that specific text with <span>

    I want to highlight or change style of specific text in post content that are same as in Tags.
    And each Tags and contents are in Japanese character.

    I tried to find some solution by myself, but nothing works… ;(

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Nathan Nawbi,

    Add this to your function.php file in your template directory

    function ncej_special_content( $id, $content ) {
    $tags = get_the_tags( $id );
    if ( $tags ) {
    
    foreach ( $tags as $tag ) {
    $old_tags[] = $tag->name;
    $new_tags[] = '<span class="special-tag">' . $tag->name . '</span>';
    }
    
    $content = apply_filters( 'the_content', $content );
    
    // Do the replacing
    $content = str_replace( $old_tags, $new_tags, $content );
    
    $content = str_replace( ']]>', ']]&gt ;', $content );
    
    echo $content;
    
    } else {
    
    $content = apply_filters( 'the_content', $content );
    
    // Do the replacing
    $content = str_replace( ']]>', ']]&gt ;', $content );
    
    echo $content;
    
    }
    }

    Now do the loop like this

    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    
    // other query here e.g the_title() and so on.
    
    // instead of using the_content() use ncej_special_content()
    
    ncej_special_content( get_the_ID(), get_the_content() );
    
    <?php endwhile; ?>
    <?php endif; ?>

    You can go ahead and style the class “special-tag” the way you like.

    I hope this helps.

    Cheers.

    Thread Starter Nathan Nawbi

    (@nathan-nawbi)

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

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

    Glad it works.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to find same text as in tags inside the post content’ is closed to new replies.