• I want to create a custom excerpt for an education website. It has posts that contains 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;
    
    }

    <?php wpden_excerpt(); ?>

    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!

    • This topic was modified 4 years, 10 months ago by laddi.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Why don’t you simply go to each post and put the HTML you want into the custom excerpt box? That’s what it’s for.

    Thread Starter laddi

    (@laddi)

    Hello Joy!
    Actually, the website already has thousands of posts so it would be like creating same amount of posts again. Also, the team who post aren’t very familiar with those tools.

    Well, it’s the WordPress way.
    This function is used in core : wp_html_split(). It might help you. There could be other functions that would help, either in WP or simply as PHP functions.
    Or you could look at plugins that pick things out of HTML, like WP Scraper and read their code.

    Moderator bcworkz

    (@bcworkz)

    If your current code extracts first paragraphs, it ought to extract first lists just as well with minor adjustment. Then do a similar process on the list so extracted to get just the first list item.

    You could use PHP’s preg_match() instead to directly get matches to a regular expression (RegExp). The RegExp is composed to only get the first list item out of ul and ol elements. This does require being able to compose RegExps. You can get a little help in doing so from online “fiddle” tools like https://regexr.com/

    Running code like this every time an excerpt is needed is not very performative. You might reconsider Joy’s suggestion to utilize the excerpt field. Not by manually adding it to thousands of posts, but by creating a script that is run once per post to do so with little intervention. Then it’s easier to fix any excerpts that the script didn’t correctly handle. Likely better than needing to code something that accurately handles every situation.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Find and print first li tag entry from WordPress post in custom excerpt?’ is closed to new replies.