Display title of child pages in shortcode

I'm having a hard time figuring out how to display the title of the childpages as the id of a div. Assuming that 'news' 'othernews' are siblings of the parent page home (39)

I want something like this:

div class="wrapper" id="news"
content
/div
div class="wrapper" id="othernews"
content
/div

This is my function for my shortcode:

function content_block( $atts, $content = null ){

extract( shortcode_atts( array(
    'type'          =      'white'

), $atts));

$childArgs = array(
    'post_type'         =      'page',
    'parent_page'       =      39
);

$child = new WP_Query($childArgs);

if ($child-have_posts()) {

    while ($child-have_posts()) {

        $child-the_post();

        if ($type == 'white') {
            $return .= 'div class="col-white"';
                $return .= 'div class="wrap" id="'. get_the_title() .'"';
                    $return .= do_shortcode($content);
                $return .= '/div';
            $return .= '/div';

        } else
        if ($type == 'grey') {
            $return .= 'div class="col-grey"';
                $return .= 'div class="wrap" id="'. get_the_title() .'"';
                    $return .= do_shortcode($content);
                $return .= '/div';
            $return .= '/div';
        }

        wp_reset_query();
        return $return;
    }
}

}

Topic shortcode child-pages Wordpress

Category Web


May I suggest you provide details as to the exact problem, and what the results are of your example code?

Also, it seems like you are saying that the pages you want to get the titles of are siblings of the parent, which is the home page, so in fact, they are not children at all (An interesting plot for a TV drama, perhaps).

In any case, instead of using the title for your ID, I would suggest using the slug (post_name) instead:

$current_id = get_the_id();
$post = get_post($current_id);
$post->post_name;

Using the title might results in spaces in your ID, which could result in an array of IDs, not just one.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.