0
votes

I added a custom php function to my wordpress template, where I want to echo the content of pages under a certain parent:

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

The code I have so far is not working as i want, the title is fine but I need to filter the content so I can grab only the text between the "<p>" tag. Is this possible? Thank you.

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;
        }




    }
}

on my php page

echo_childs_of( 7 );
2

2 Answers

0
votes

You could use DOM to find the p tag elements and itarate over them using a for cycle to do whatever it is that you want to do.

$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
votes

Try this:

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