pulling out list from wordpress post

2019-09-13 14:21发布

I want to pull out the list (ul) element from my wordpress post(s) so I can put it in a different location.

My current css pulls out the images and blockqute and puts just the text

html

<?php
  $content = preg_replace('/<blockquote>(.*?)<\/blockquote>/', '', get_the_content());
  $content = preg_replace('/(<img [^>]*>)/', '', $content);
  $content = wpautop($content); // Add paragraph-tags
  $content = str_replace('<p></p>', '', $content); // remove empty paragraphs
    echo $content;
?>    

1条回答
一夜七次
2楼-- · 2019-09-13 14:59

Just a friendly reminder is that it is generally not recommended to parse html with regex. If you would like to do that anyway you could try like this:

$pattern = '~<ul>(.*?)</ul>~s';

So in your code it would look like this:

preg_match_all('/(~<ul>(.*?)</ul>~s)/', $content, $ulElements);

And then for removing it from the original string:

preg_replace('/(~<ul>(.*?)</ul>~s)/', '', $content);
查看更多
登录 后发表回答