WordPress.org

Ready to get started?Download WordPress

Forums

Child Page Navigation
Enhancement: Show parent page if needed ;-) (2 posts)

  1. sixtyseven
    Member
    Posted 1 year ago #

    Good work with your widget. In my case, I needed to display the parent page under some cirumstances, so I made a quick enhancement:

    <?php
    /*
        Plugin Name: Child Page Navigation
        Plugin URI: http://wordpress.org/extend/plugins/child-page-navigation/
        Description: Provides a theme-independent widget to display the child pages of the current page.
        Author: ITS Alaska
        Author URI: http://ITSCanFixThat.com/
        Version: 1.2.1
    
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */
    
        class theme_navigation extends WP_Widget {
            function theme_navigation() {
                parent::WP_Widget('theme_navigation', 'Child Page Navigation', array('description' => '', 'class' => 'theme-navigation'));
            }
            function form($instance) {
                $default =     array( 'title' => __('Navigation'), 'show_parent' => 0 );
                $instance = wp_parse_args( (array) $instance, $default );
                $field_id = $this->get_field_id('title');
                $field_name = $this->get_field_name('title');
                echo "\r\n".'<p><label for="'.$field_id.'">'.__('Title').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.attribute_escape( $instance['title'] ).'" /><label></p>';
    			echo "\r\n".'<p><input class="checkbox" type="checkbox" value="1" ';
    			checked( $instance['show_parent'], 1 );
    			echo ' id="'.$this->get_field_id( 'show_parent' ).'" name="'.$this->get_field_name( 'show_parent' ).'" />
    			<label for="'.$this->get_field_id( 'show_parent' ).'">Show parent page?</label></p>';
                $field_id = $this->get_field_id('page');
            }
            function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance['title'] = strip_tags($new_instance['title']);
    			$instance['show_parent'] = $new_instance['show_parent'];
                return $instance;
            }
            function widget($args, $instance) {
                extract($args, EXTR_SKIP);
                global $post;
                $pages = get_pages(array(
                    'child_of' => $post->ID,
                    'parent' => $post->ID,
                    'sort_column' => 'menu_order'
                ));
    
    			// Chek if Parent should be displayed
    			$show_parent = isset( $instance['show_parent'] ) ? $instance['show_parent'] : false;
    
                // Check if the current page has a parent
                if ($post -> post_parent) {
                    // Get sibling pages
                    $siblings = get_pages(array(
                        'child_of' => $post -> post_parent,
                        'parent' => $post -> post_parent,
                        'sort_column' => 'menu_order'
                    ));
                }
    
                if(count($pages)){
                    echo $before_widget;
                    $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
                    if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
                    echo "<ul>";
    				if ( $show_parent  == 1){
    					echo "<li><a href='".get_permalink($page->post_parent)."'>".get_the_title($page->post_parent)."</a></li>";
    				}
                    foreach($pages as $page){
                        echo "<li><a href='".get_permalink($page->ID)."'>".get_the_title($page->ID)."</a></li>";
                    }
                    echo "</ul>";
                    echo $after_widget;
                }elseif($post -> post_parent) { // Show the sibling pages if there are no children
                    if(count($siblings)) {
                        echo $before_widget;
                        $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
                        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
                        echo "<ul>";
    					if ( $show_parent == 1){
    						echo "<li><a href='".get_permalink($post -> post_parent)."'>".get_the_title($post -> post_parent)."</a></li>";
    					}
                        foreach($siblings as $page){
                            echo "<li><a href='".get_permalink($page->ID)."'>".get_the_title($page->ID)."</a></li>";
                        }
                        echo "</ul>";
                        echo $after_widget;
                    }
                }
            }
        }
        function register_theme_navigation_widget(){
            register_widget('theme_navigation');
        }
        add_action('widgets_init','register_theme_navigation_widget');
    
    ?>

    Just in case someone could make use of this.

  2. JohnStu
    Member
    Posted 1 year ago #

    Great addition. Thanks a lot.
    I just wondering is it possible that instead plugin title (e.g. Navigation) be name of that main page.
    Thanks in advance.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic