I am using the following custom WordPress shortcode to display a list on a page, I am trying to find the 5th LI element and inject a DIV at this point.
[expander]
<ul>
<li>step1</li>
<li>step 2</li>
<li>step 3</li>
<li>step 4</li>
<li>step 5</li>
<li>step 6</li>
<li>step 7</li>
</ul>
[/expander]
Shortcode:
add_shortcode( 'expander', 'my_function' );
function my_function($atts, $content) {
echo substr_count($content, '<li>'); // 7
return $content;
}
Intended Output:
<ul>
<li>step1</li>
<li>step 2</li>
<li>step 3</li>
<li>step 4</li>
<li>step 5</li>
<div>
<li>step 6</li>
<li>step 7</li>
</div>
</ul>