Showing Child / Sub Pages on a Page – One Level Deep

It was  a problem i faced recently with wp_list_pages() function. I was trying to pull sub pages for a page to be displayed on the sidebar. The function wp_list_pages() would allow me to show child pages but it would go to unlimited depth of the page tree and retrieve all pages of the ancestor page.

So i created the following widget to resolve this.


<?php

class RLSubpages_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct(

'rlsubpages_widget',
'RL Subpages Widget',
array( 'description' => __( 'A Subpages Widget', 'text_domain' ) )
);
}

public function form( $instance )
{
if ( isset( $instance['title'] ) )
{
$title = $instance['title'];
}
else
{
$title = __( 'Subpages', 'text_domain' );
}

if ( isset( $instance['rl_show_title'] ) )
{
$rl_show_title = $instance['rl_show_title'];
}
else
{
$rl_show_title = 1;
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>

<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'rl_show_title' ); ?>"><?php _e( 'Show Title?:' ); ?></label>

<select class="widefat" id="<?php echo $this->get_field_id( 'rl_show_title' ); ?>" name="<?php echo $this->get_field_name( 'rl_show_title' ); ?>" >
<option value='1' <?php if ( $rl_show_title == 1 ) echo "SELECTED"; ?>>Show</option>
<option value='0' <?php if ( $rl_show_title == 0 ) echo "SELECTED"; ?>>Hide</option>
</select>

</p>

<?php
}

public function update( $new_instance, $old_instance )
{
$instance = array();

$instance['title'] = strip_tags( $new_instance['title'] );

$instance['rl_show_title'] = strip_tags( $new_instance['rl_show_title'] );

return $instance;
}

public function widget( $args, $instance )
{
extract( $args );

$title = apply_filters( 'widget_title', $instance['title'] );

$show_title = $instance['rl_show_title'];

echo $before_widget;

if ( ! empty( $title ) && $show_title == 1 )

echo $before_title . $title . $after_title;

if ( is_page() )
{
global $id;

$pages = get_pages ('child_of='.$id.'&parent='.$id);

foreach ($pages as $page)
{
?>
<li><a href="<?php echo get_permalink($page->ID) ?>"><?php
echo $page->post_title ?></a></li>
<?php
}
}

echo $after_widget;

}
}

add_action( 'widgets_init', create_function( '', 'register_widget( "RLSubpages_Widget" );' ) );

Place the above code in your theme’s functions.php file. Next, go to theme>appearance>widgets, you will see ‘RL Subpages Widget’ listed there. Pull it to your sidebar. done.

For any issues post a comment below.

VN:F [1.9.12_1141]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.12_1141]
Rating: 0 (from 0 votes)
Showing Child / Sub Pages on a Page - One Level Deep, 10.0 out of 10 based on 1 rating
This entry was posted in General. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

You must be logged in to post a comment.