使用DOMDocument 和DOMXPath 你可以试试这个。
<?php while (have_posts()) : the_post();
ob_start(); // run the_content() through the Output Buffer
the_content();
$html = ob_get_clean(); // Store the formatted HTML
$content = new DomDocument(); // Create a new DOMDocument Object to work with our HTML
$content->loadHTML( $html ); // Load the $html into the new object
$finder = new DomXPath( $content ); // Create a new DOMXPath object with our $content object that we will evaluate
$classname = \'ps_post_description\'; // The class we are looking for
$item_list = $finder->query("//*[contains(@class, \'$classname\')]"); // Evaluates our content and will return an object containing the number of items matching our query
?>
<li style = "text-align: center; float : left;">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo get_the_post_thumbnail($id); ?></a>
<div class="title">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</div>
<div id = "result">`
<?php
// echo the value of each item found
// You might want to format or wrap your $value according to your specification if necessary
for( $i = 0; $i < $item_list->length; $i++ ){
$value = $item_list->item( $i )->nodeValue;
echo $value;
// if you want the text to be a link to the post, you could use this instead
// echo \'<a href="\' . get_the_permalink() . \'">\' . $value . \'</a>\';
} ?>
</div>
</li>
<?php endwhile; ?>
UPDATE
利用
Output Buffering 在尝试在DOMDocument中加载HTML之前展开短代码
请注意,这两种解决方案(编辑之前和之后)都有效,因此错误/警告应该来自设置中的其他地方。