Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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();
share|improve this answer
    
I add this <?php $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> endwhile; wp_reset_postdata(); ?> // Browser show Parse error: syntax error, unexpected '<' –  ogni Jun 13 '12 at 14:32
    
@ogni ah... sorry... forget about it. i have updated my answer, check it again. –  Eugene Manuilov Jun 13 '12 at 14:34
    
Thanks works, but i would like to show on the home-page a lot of different sub-page titles (overview-page), at the moment every item has the same title-name? –  ogni Jun 13 '12 at 14:45
    
it's hard to tell you what's wrong, without seeing your page. [sarcasm]my rating doesn't allow me to read your mind remotely[/sarcasm] –  Eugene Manuilov Jun 13 '12 at 14:48
    
I make a screenshot with comments and load it up -j ust a moment, please –  ogni Jun 13 '12 at 14:57
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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