最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Find and print first <li> entry from Wordpress post in custom excerpt?

programmeradmin4浏览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!

发布评论

评论列表(0)

  1. 暂无评论