• Resolved emangham

    (@emangham)


    Hello,

    I have a ‘wp_list_pages’ on my page that list other child pages with the same parent, excluding the current one. This list is also limited to 3 items. Both of these arguments are not standard ones so I’m struggling to add on a thumbnail.

    Here’s my code:

    <?php
       $limit_similar = 3;
       $pages = wp_list_pages("echo=0&title_li=&exclude=".$post->ID."&child_of=".$post->post_parent);
       $pages_arr = explode("\n", $pages);
    ?>
    <?php for ($i=0; $i<$limit_similar; $i++) { ?>
       <?php echo $pages_arr[$i]; ?>
    <?php } ?>

    This works perfectly, although all it is doing is displaying them in an a very standard list.

    There’s two things I’d like to do, 1) can I have more flexibility on the code that it outputs for each page? And 2) can it include the page thumbnail for each one please?

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You probably want a custom query using WP_Query. So you could do something like this:

    <?php
    global $post;
    $siblings = new WP_Query( array(
        'post_type'      => 'page',
        'order'          => 'ASC',
        'orderby'        => 'menu_order',
        'posts_per_page' => 3,
        'no_found_rows'  => true,
        'post_parent'    => $post->post_parent,
        'post__not_in'   => array( $post->ID ),
    ) );
    
    if ( $siblings->have_posts() ) : ?>
        <!-- A container element can go here. -->
            <?php while( $siblings->have_posts() ) : $siblings->the_post(); ?>
                <!-- Use template tags here to display post data. -->
            <?php endwhile; ?>
        <!-- Close container element. -->
    <?php endif; wp_reset_query(); ?>

    Where it says <!-- Use template tags here to display post data. --> you can use template tags like the_title(), the_permalink(), the_post_thumbnail(), and the_content() to display posts however you want.

    Thread Starter emangham

    (@emangham)

    Jacob, you sir are fantastic. Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add thumbnail to 'my wp_list_pages' with limit and exclude’ is closed to new replies.