我在我的wordpress模板中添加了一个自定义的php函数,我希望在其中回显某个父元素下的页面内容:
Departments
-department 1 (get the title and the content clean)
-department 2 (get the title and the content clean)
(...)
到目前为止我的代码没有按照我想要的方式工作,标题很好,但我需要过滤内容,这样我才能抓取"<p>"
标记之间的文本。这可能吗?谢谢。
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 :(得分:0)
您可以使用DOM来查找p标记元素,并使用for循环来覆盖它们,以执行您想要执行的操作。
$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?
}
答案 1 :(得分:0)
试试这个:
$content = get_the_content($child);
preg_match('/<p>(.*)<\/p>/', $content, $match);
$content = $match[1];