I intend to create a sidebar to retrieve a list of the current page's children as well as the current page itself. Here is the code that I have been using which retrieves the current pages children even when on the child pages, however when I try to include the $parent
as $post->post_parent
it displays all pages
<?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 } ?>
/*-------------------------------------------------------------------
Return parent page ID or child as else. Add this to functions.php
---------------------------------------------------------------------*/
function get_top_parent_page_id() {
global $post;
if ($post->ancestors) {
return end($post->ancestors);
} else {
return $post->ID;
}
}
//Use this in the template
<?php while ( have_posts() ) : the_post();
//Add parent page slug into arguments
$query = new WP_Query('pagename=page_slug');
if ($query->have_posts() ){ while ($query->have_posts() ) { $query->the_post(); } }
//Get children by calling function in functions.php for the top parent id
$current_page_id = get_top_parent_page_id();
$args = array( 'post_type' => 'safaris', 'post_parent' => $current_page_id, 'orderby' => 'title', 'order' => 'ASC' );
$child_query = new WP_Query($args);
$i = 0;
if ($child_query->have_posts() ){ while ($child_query->have_posts() ){ $child_query->the_post();
$i++; if( 1 == $i ) { ?>
<li role="presentation" class="active"><?php the_title(); ?></li>
<?php } else { ?>
<li role="presentation"><?php the_title(); ?></li>
<?php } } } wp_reset_postdata(); endwhile; ?>
Here is the code that worked.
<?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 } ?>