2016-10-11 100 views
0

我有下面的代碼,我試圖從我的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> 

但內容不會出現。

+0

您必須使用strip_tag從內容中移除html標籤。 http://www.w3schools.com/php/func_string_strip_tags.asp –

回答

0

我覺得你太過分了,因爲只是檢索一篇文章。爲什麼不使用the_content();循環內?

get_the_content()不會自動嵌入視頻,或擴展簡碼,除其他事項外 - 我能看到你重新裝入一個HTML格式

+0

我想刪除在PHP標籤全部都在崗位用什麼方式藏漢 – user3550879

0

嘗試用這種

use the_content(); 
+0

圖像和引用文字,<?PHP中使用the_content() ; ?> – user3550879