• I’m trying to list child pages on the parent page in alphabetical order, I can get the list to show using the following code (directly in the page template) but they aren’t in order:

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

    If you know of a better more efficient way please let me know. I need to show a list of children with thumbnails for the CURRENT page.

    • This topic was modified 9 years, 1 month ago by YOELO.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,
    Please use my this code instead of your code, i have corrected your code.

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

    Thanks
    Mohammad

    Thread Starter YOELO

    (@yoebo)

    Thanks Mohammad but alas that doesn’t change the order.

    Hi,
    Please try to use this code:-

    
    <?
    //$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY 'title' ASC", 'OBJECT');
    $child_pages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'title', 'sort_order' => 'asc' ) );
    
    if ( $child_pages ) :
        foreach ( $child_pages as $pageChild ) :
            setup_postdata( $pageChild );
            $thumbnail = get_the_post_thumbnail($pageChild->ID, 'post-thumb');
    ?>
            <div class="child-thumb">
              <a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark" title="<?= $pageChild->post_title ?>">
    	<?= $thumbnail ?>
    	
                <?= $pageChild->post_title ?>
              </a>
            </div>
    <?
        endforeach;
    endif; ?>

    Thanks
    Mohammaad

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Child Pages Listed Alphabetically’ is closed to new replies.