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;



<p>Once in a blue moon</p>
  <li>not very often</li>
  <li>once after a long time</li>
  <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>

Now I want to print something like that:

<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> -->

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

  • 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


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.