我打算创建一个侧边栏来检索当前页面的子列表以及当前页面本身。以下是我一直使用的代码,即使在子页面上也会检索当前页面的子代码,但是当我尝试将$parent
包含为$post->post_parent
时,它会显示所有页面
<?php
if ($post->post_parent) {
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
$children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");
$parent = wp_list_pages("title_li=&child_of=". $post->post_parent ."&echo=0");
if ($children) { ?>
<ul class="tabs">
<?php echo $parent ?>
<?php echo $children; ?>
</ul>
<?php } ?>
答案 0 :(得分:0)
这是有效的代码。
<?php if ( !is_page()) { ?>
<div class="card recentPosts">
<h3>Recent posts</h3>
<?php
$args = array( 'numberposts' => 4, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumbnail', true);
?>
<a class="recentposts" href="<?php the_permalink(); ?>" title="<?php the_title();?>">
<?php
if ( has_post_thumbnail() ) {
echo '<div class="aside image">';
the_post_thumbnail( 'sidebarThumb' );
echo '</div>';
}
?>
<div class="title">
<h3><?php the_title(); ?></h3>
<div class="meta">
<div class="metaItem postedon">
<?php echo get_the_date('F y'); ?>
</div>
<div class="metaItem author">
<?php
global $post;
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$full_name = '';
if( empty($fname)){
$full_name = $lname;
} elseif( empty( $lname )){
$full_name = $fname;
} else {
//both first name and last name are present
$full_name = "{$fname} {$lname}";
}
echo 'By '.$full_name;
?>
</div>
</div>
</div>
<div class="feature-image" style="background-image: url(<?php echo $image_url[0]; ?> );"></div>
</a>
<hr />
<?php endforeach;
wp_reset_postdata();
?>
</div>
<?php } ?>