admin管理员组

文章数量:1122846

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

}

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

}
Share Improve this question asked Nov 20, 2014 at 14:34 Jeroen BellemansJeroen Bellemans 1011 bronze badge 2
  • there is no parent_page, it's post_parent. and you reset the query and return inside of your while loop, so it can't possibly run for more than 1 iteration before exiting. – Milo Commented Nov 20, 2014 at 16:35
  • Thanks for the reply, I figured it out. Thanks anyways :) – Jeroen Bellemans Commented Nov 21, 2014 at 15:01
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: Display title of child pages in shortcode