0

I am trying to Limit Maximum Numbers of Tag Links to appear within post content. Below is my code. I don't know how to fix it up.

function link_words( $text ) {
 $replace_times=3;

    $replace = array();
    $tags = get_tags();

    if ( $tags ) {
        foreach ( $tags as $tag ) {
            $replace[ $tag->name, $replace_times ] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
        }
    }

    $text = str_replace( array_keys($replace), $replace, $text );
    return $text;
}
add_filter( 'the_content', 'link_words' );
0
function link_words( $text ) {
$replace = '';
$tags = get_the_tags();
$count=0;
if ( $tags ) {
    foreach ( $tags as $tag ) {
        $count++;
        echo $sep . '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a>';
        $sep = ', ';
          if( $count > 2 ) break;
    }
}

} add_filter( 'the_content', 'link_words', 10, 1 );

Check this.

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.