我有下面的代碼,我試圖從我的wordpress文章中提取文本,並且只能回顯div中的文本內容。 (我從後去除引用文字,圖片等,以在別處使用)WordPress的內容(文本)沒有顯示
<?php
$content = get_the_content();
$content = wpautop($content);
$doc = new DOMDocument();
$doc->loadHTML(get_the_content(), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//blockquote') as $node) {
$node->parentNode->removeChild($node);
}
foreach ($xpath->query('//img') as $node) {
$node->parentNode->removeChild($node);
}
foreach($xpath->query('//p[not(node())]') as $node) {
$node->parentNode->removeChild($node);
}
$content = $doc->saveHTML($doc);
?>
<div>
<?php echo $content ?>
</div>
但內容不會出現。
您必須使用strip_tag從內容中移除html標籤。 http://www.w3schools.com/php/func_string_strip_tags.asp –