2

i'm query child pages of a page ID. How can i echo the parent page where it says "PARENT TITLE ? Using <?php echo get_the_title( $ID ); ?> is not an option.

<?php

$args = array(
    'post_type'      => 'page', //write slug of post type
    'posts_per_page' => -1,
    'post_parent'    => '2', //place here id of your parent page
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


$childrens = new WP_Query( $args );

if ( $childrens->have_posts() ) : ?>


    <div class="content">
        <h2 class="big grid_3">PARENT TITLE</h2>
        <div class="grid_6 right">
        <?php while ( $childrens->have_posts() ) : $childrens->the_post(); ?>

        <div class="grid_2 left" id="teaser" >
            <p class="subtitle"></p>
            <h3><?php the_title(); ?></h3>
            <?php the_excerpt(); ?>
            <a class="cta_light" href="<?php the_permalink(); ?>">Mehr erfahren</a>

        </div>

    </div>
    </div>
<?php
        endwhile; 
endif; 
wp_reset_query(); 
?>

Thanks!

1

1 Answer 1

2

I'm not entirely sure if I get your problem correctly, because of the "it's not an option part", but...

You get children pages of page with ID = 2, so it looks like you want to display the title of that page. If so, then this code will solve your problem:

<?php
    $args = array(
        'post_type'      => 'page', //write slug of post type
        'posts_per_page' => -1,
        'post_parent'    => '2', //place here id of your parent page
        'order'          => 'ASC',
        'orderby'        => 'menu_order'
    );

    $childrens = new WP_Query( $args );

    if ( $childrens->have_posts() ) :
?>
    <div class="content">
        <h2 class="big grid_3"><?php echo get_the_title(2); ?></h2>
        <div class="grid_6 right">
            <?php while ( $childrens->have_posts() ) : $childrens->the_post(); ?>
            <div class="grid_2 left" id="teaser" >
                <p class="subtitle"></p>
                <h3><?php the_title(); ?></h3>
                <?php the_excerpt(); ?>
                <a class="cta_light" href="<?php the_permalink(); ?>">Mehr erfahren</a>
            </div>
            <?php endwhile; // <- I had to move that, it was in wrong place so you were closing more divs than you were opening in that loop ?>
        </div>
    </div><!-- .content -->
<?php
    endif; 
    // wp_reset_query();  <- there is no point in resetting query - you haven't changed it
    wp_reset_postdata(); // on the other hand you should reset postdata
?>

PS. Also take a look at my comments - I had to make some changes in your code, because it was generating incorrect HTML output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.