0

I want to create a custom excerpt for an education website. It has posts that contains and listing. I want to print the first child of ul and a first child of ol tag from the post.

Currently, I have the following code that can print only the first paragraph.

function wpden_excerpt()
{
    global $post;

   $output = get_the_content();

$wanted_number_of_paragraph = 1;

$tmp = explode ('</p>', $output);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
   if (isset($tmp[$i]) && $tmp[$i] != '') {
       $tmp_to_add[$i] = $tmp[$i];
   }
}
$output = implode('</p>', $tmp_to_add) . '</p>';

    echo $output;

}

HTML

<p>Once in a blue moon</p>
<p>Meaning</p>
<ul>
  <li>not very often</li>
  <li>rarely</li>
  <li>once after a long time</li>
</ul>
<p>Examples</p>
<ol>
  <li>My sister lives in Alaska, so I only see her once in a blue moon.</li>
  <li>Once in a blue moon, there's an issue I can't resolve.</li>
  <li>That company puts on a good performance only once in a blue moon.</li>
</ol>

Now I want to print something like that:

<p>
<b>Meaning:</b> not very often<br /> <!-- Content from first <li> of first <ul> -->
<b>Example:</b> My sister lives in Alaska, so I only see her once in a blue moon. <!-- Content from first <li> of first <ol> -->
</p>

I've tried a lot, but I am not getting succeeded. Plz help!

2
  • If you ignore the excerpt and content, and just say you have HTML in a variable, and want to extract stuff from it to give something else, then it becomes obvious that no WP knowledge is needed to answer this, and it's a pure PHP problem. If you phrase it that way you can ask on stackoverflow and get a lot more eyes on it and a higher chance of an answer
    – Tom J Nowell
    Apr 26, 2020 at 16:58
  • Thanks for suggestion Tom! :) Apr 26, 2020 at 17:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.