$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Query children and parent title|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Query children and parent title

matteradmin10PV0评论

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!

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!

Share Improve this question edited Jan 31, 2019 at 21:18 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 31, 2019 at 20:47 baalstormbaalstorm 211 bronze badge 1
  • Why isn’t it an option? – Krzysiek Dróżdż Commented Jan 31, 2019 at 20:51
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far