I want to add new conditions to the following code:
function add_thumb_after_h2( $content ) { if (is_single()) { global $post; if ( substr_count( $content, '<h2>' ) > 3 ) { $thumb = '<div id="post_thumbnail">' . get_the_post_thumbnail( $post->ID, 'fullsize' ) . '</div>'; $content = str_replace_once("</h2>", "</h2>".$thumb, $content); } } return $content; }
I also need to include h3
, h4
and h5
, example:
if ( substr_count( $content, '<h2>,<h3>,<h4>,<h5>' ) > 3 ) {}
How can I do it?
The thumbnail should be added only to the first occurrence of a heading tag, regardless of its type (h2
, h3
, etc.)
Thanks
Advertisement
Answer
You can achieve this my modifying your function to the following:
function add_thumb_after_h2( $content ) { if ( is_single() ) { global $post; foreach ( array('h2', 'h3', 'h4', 'h5') as $heading ) { if ( ! $replaced && substr_count( $content, "<{$heading}>" ) > 3 ) { $thumb = '<div id="post_thumbnail">' . get_the_post_thumbnail( $post->ID, 'fullsize' ) . '</div>'; $content = str_replace_once("</{$heading}>", "</{$heading}>" . $thumb, $content); break; } } } return $content; }