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' );