I have the following:
<?php
$content = apply_filters( 'the_content', get_the_content() );
$content_table = explode("<p>", $content);
$content_table[3] .= $spot1;
$content_table[6] .= $spot2;
$content_table[9] .= $spot3;
$content_table[12] .= $spot4;
$content_table[15] .= $spot5;
$content2 = implode($content_table, "<p>");
echo $content2;
?>
This grabs the content from the page (WordPress) and then after each 3rd paragraph inserts a custom shortcode (ie. $spot1) – this works great, but it only applies to paragraphs. How do I also include H2 tags? Because it only applies to paragraphs I find that the $spot1 comes after the heading – but I want to include headings in the equation – so that it counts these as well.
2
Answers
We can build a custom function that will act as a multi-separators explode using
str_replace()
andexplode()
.On the front-end, we can call our function to parse our content.