Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to display other child pages in a specific section so I found this 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" style="">
<h3 class="widgettitle">Also in this Section</h3>
<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,'full'); ?>
            <?php echo get_the_excerpt($page->post_excerpt); ?>

          </li>
        <?php endforeach ?>
       <?php endif ?>
       </ul>
 </div>
<?php } ?>

I want it to display the except per other child pages but it's showing the excerpt for the current page. I have added page excerpts in my functions file to display page excerpts in pages. Can anyone point out what I'm missing in my code? Any help would be appreciated as I'm new to Wordpress. thanks.

question from:https://stackoverflow.com/questions/66052560/display-page-excerpt-in-wordpress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

There is a typo in your code using $page instead of $page_item. Moreover you are using get_the_excerpt() and use the excerpt itself as the parameter for the function. This is not how the function works: https://developer.wordpress.org/reference/functions/get_the_excerpt/

This line is the problem :

<?php echo get_the_excerpt($page->post_excerpt); ?>

You can do:

<?php echo $page_item->post_excerpt; ?>

Or you could also do:

<?php echo get_the_excerpt($page_item->ID); ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...