2016-01-20 126 views
0

我想只顯示從一個站點到另一個站點的特定內容。WordPress的:從另一個頁面獲取指定的內容

例如:
我想在<div> @其他頁面中顯示一些文本。

頁答:

<h2>This is page A</h2> 

<div id="showonly">This is the content I want to show in footer for ex.</div> 

頁腳:
現在我想只顯示與此特定ID股利。

我做了什麼至今:

  1. 這是我從頁面獲取內容(全部內容)

    function show_post_leweb($path) { 
        $post = get_page_by_path($path);  
        $content = apply_filters('the_content', $post->post_content);  
        echo $content;  
    } 
    
  2. 在footer.php我沒有那麼功能:

    $content_leweb = show_post_leweb('kontakt'); 
    echo '<div class="special-content">' .$content_leweb . '</div>'; 
    

工作正常,但我只需要一個特定的div而不是整個頁面,就像我上面提到的那樣。

回答

0

您需要使用preg_match來自輸出的特定內容。

所以使用preg_match。把條件的開始...結束div。那麼你將只在你的輸出中獲得div內容。

有關更多信息,請參閱http://php.net/manual/en/function.preg-match.php

首先讓你的正則表達式不那麼貪婪?運營商。這將確保你不會得到更多,然後你認爲你會得到。接下來的事情是你的表達式結尾處的/ s操作符。我假設你解析了一個HTML文件,其中有許多新的行字符「\ n」。

$value=preg_match_all('/<div class=\"name\-of\-class\">(.*?)<\/div>/s',$file_contents,$estimates); 
相關問題