0

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

1 Answer 1

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();
6
  • 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, 2012 at 14:32
  • @ogni ah... sorry... forget about it. i have updated my answer, check it again. Jun 13, 2012 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, 2012 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] Jun 13, 2012 at 14:48
  • I make a screenshot with comments and load it up -j ust a moment, please
    – ogni
    Jun 13, 2012 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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