How do I target the immediate paragraph after the first h2 tag in every post? (They have been structured a specific way)
<!-- Example 1 -->
<p>Lorem Ipsum</p>
<p>Lorem Ipsum</p>
<h2>Title</h2>
<p>Lorem Ipsum</p><!-- PHP gets this paragraph -->
<p>Lorem Ipsum</p>
<!-- Example 2 -->
<p>Lorem Ipsum</p>
<p>Lorem Ipsum</p>
<h2>Title</h2>
<p>Lorem Ipsum</p><!-- PHP gets this paragraph -->
<p>Lorem Ipsum</p>
<h2>Title</h2>
<p>Lorem Ipsum</p>
Normally I would use preg_match or preg_split, but Wordpress doesn't store the post_content with p tags I can target, so that didn't seem viable.
EDIT:
I figured out how to do this, the code below works:
<?php
$texts = preg_split( '/\r\n|\r|\n/', get_the_content() );
// Loop through items
foreach($texts as $text) {
// If $stop has been set true by presence of H2 in previous item, then break after echoing paragraph
if ($stop == true) {
echo $text;
break;
}
// If first h2 present, then set $stop to true to stop loop after next item
if (strpos($text, 'h2')) {
$stop = true;
}
}
` tags... If you are using Markdown then I guess you could use RegEx producing something horrible like this: https://regex101.com/r/5BCiUk/1 (>_<), however MD has different ways of writing headings (`##` under the line for instance). I strongly recommend reading up on: http://php.net/manual/en/function.simplexml-load-string.php
– JustCarty Mar 01 '19 at 15:09