admin管理员组

文章数量:1297088

how can I do this:

Pages:

Home
- sub-1
-- sub-sub-1
--- sub-sub-sub-1

On home-page should only show the page-title of the first child-page like: sub-1

I found this:

<?php $child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php endforeach; endif; ?>

But it doesn´t work for me at all. The title will be show two times and show all child-page titles (sub-1, sub-sub-1, sub-sub-sub-1)

Thanks
Ogni

how can I do this:

Pages:

Home
- sub-1
-- sub-sub-1
--- sub-sub-sub-1

On home-page should only show the page-title of the first child-page like: sub-1

I found this:

<?php $child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php endforeach; endif; ?>

But it doesn´t work for me at all. The title will be show two times and show all child-page titles (sub-1, sub-sub-1, sub-sub-sub-1)

Thanks
Ogni

Share Improve this question asked Jun 13, 2012 at 14:04 ogniogni 5821 gold badge9 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use WP_Query class to do it:

$the_query = new WP_Query( array( 
    'post_parent'    => $post->ID,
    'post_type'      => 'page',
    'posts_per_page' => 1,
) );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2 class="subpagetitle">
        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
            <?php the_title(); ?>
        </a>
    </h2>
<?php endwhile;

// Reset Post Data
wp_reset_postdata();

本文标签: Show page title just from the first childpage in template