$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How To List Sibling Pages And Include The Featured Image?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How To List Sibling Pages And Include The Featured Image?

matteradmin8PV0评论

I recently came across this cool tutorial on listing sibling pages. Below is the suggested code:

<?php
    //GET CHILD PAGES IF THERE ARE ANY
    $children = get_pages('child_of='.$post->ID);
 
    //GET PARENT PAGE IF THERE IS ONE
    $parent = $post->post_parent;
 
    //DO WE HAVE SIBLINGS?
    $siblings =  get_pages('child_of='.$parent);
 
    if( count($children) != 0) {
       $args = array(
         'depth' => 1,
         'title_li' => '',
         'child_of' => $post->ID
       );
 
    } elseif($parent != 0) {
        $args = array(
             'depth' => 1,
             'title_li' => '',
             'exclude' => $post->ID,
             'child_of' => $parent
           );
    }
    //Show pages if this page has more than one sibling 
    // and if it has children 
    if(count($siblings) > 1 && !is_null($args))   
    {?>
    <div class="widget subpages">
    <h3 class="widgettitle">Also in this Section</h3>
         <ul class="pages-list">
         <?php wp_list_pages($args);  ?>
         </ul>
     </div>
    <?php } ?>

I was wondering how can I can modify the above code snippet to include the featured image, post title, and permalink. I would like to imitate "related posts".

Looking forward to your input!

I recently came across this cool tutorial on listing sibling pages. Below is the suggested code:

<?php
    //GET CHILD PAGES IF THERE ARE ANY
    $children = get_pages('child_of='.$post->ID);
 
    //GET PARENT PAGE IF THERE IS ONE
    $parent = $post->post_parent;
 
    //DO WE HAVE SIBLINGS?
    $siblings =  get_pages('child_of='.$parent);
 
    if( count($children) != 0) {
       $args = array(
         'depth' => 1,
         'title_li' => '',
         'child_of' => $post->ID
       );
 
    } elseif($parent != 0) {
        $args = array(
             'depth' => 1,
             'title_li' => '',
             'exclude' => $post->ID,
             'child_of' => $parent
           );
    }
    //Show pages if this page has more than one sibling 
    // and if it has children 
    if(count($siblings) > 1 && !is_null($args))   
    {?>
    <div class="widget subpages">
    <h3 class="widgettitle">Also in this Section</h3>
         <ul class="pages-list">
         <?php wp_list_pages($args);  ?>
         </ul>
     </div>
    <?php } ?>

I was wondering how can I can modify the above code snippet to include the featured image, post title, and permalink. I would like to imitate "related posts".

Looking forward to your input!

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Jun 6, 2014 at 5:53 WilliamWilliam 2071 gold badge4 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

wp_list_pages only displays page title and link in list. Try following code to fetch page content and thumbnail.

<ul class="pages-list">
           <?php $our_pages = get_pages($args); ?>
           <?php if (!empty($our_pages)): ?>
            <?php foreach ($our_pages as $key => $page_item): ?>
              <li>
                <a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>"><?php echo $page_item->post_title ; ?></a>
                <?php echo get_the_post_thumbnail($page_item->ID,'thumbnail'); ?>
              </li>
            <?php endforeach ?>
           <?php endif ?>
           </ul>
Post a comment

comment list (0)

  1. No comments so far