2015-12-16 34 views
0

我添加了一個自定義的PHP函數,我的wordpress模板,在這裏我想呼應的頁面一定母公司下的內容:WordPress的PHP內容格式

Departments 
    -department 1 (get the title and the content clean) 
    -department 2 (get the title and the content clean) 
    (...) 

我到目前爲止的代碼不工作,我希望,標題是好的,但我需要過濾的內容,所以我只能抓住"<p>"標籤之間的文本。這可能嗎?謝謝。

的functions.php

function echo_childs_of($postID) { 
    $args = array(
     'order' => 'ASC', 
     'post_parent' => $postID, 
     'post_status' => null, 
     'post_type' => 'any' 
    ); 

    $page_childs = get_children($args); 

    if ($page_childs) { 

     foreach ($page_childs as $child) { 

      $title = get_the_title($child); 
      $content = get_the_content($child); 
      $content = strip_shortcodes($content); 
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content); 
      $content = strip_tags($content); 

      echo $title; 
      echo $content; 
     } 




    } 
} 

我的PHP頁面上

echo_childs_of(7); 

回答

0

你可以使用DOM使用for循環做什麼,那就是你找到了他們的p標籤的元素和itarate想做。

$content = get_the_content($child); 
$doc = new DOMDocument(); 
@$doc->loadHTML($content); 
$p_elements = $doc->getElementsByTagName('p'); 
foreach ($p_elements as $p) { 
       //Do something with the p element... Strip tags maybe? 
} 
+0

@PeterSmith我得到的錯誤:**無法加載資源:服務器迴應500(內部服務器錯誤)的狀態** –

+0

的http:// pastebin.com/raw/zFF7zu93 –

0

試試這個:

$content = get_the_content($child); 
preg_match('/<p>(.*)<\/p>/', $content, $match); 
$content = $match[1];