WordPress.org

Forums

[resolved] Widget using WP_list_pages (display result problem) (3 posts)

  1. kzee
    Member
    Posted 4 years ago #

    Hello,

    I was looking for a wp_list_pages function which display a "Child Pages" when there is one (or more), and display "Same Level Pages" where is no "Child Pages".

    I found that (which is working when hardcoded) :

    [Code moderated as per the Forum Rules. Please use the pastebin]

    The result is always and only displaying the toplevel page.
    The widget form only allow to change the limit of result to display and the title ($instance["limit"] and $instance["title"]).

    Thanks.

  2. kzee
    Member
    Posted 4 years ago #

    Hardcoded :

    //Limit number to display
    $nbpages = 5;
    //Request
    $pages = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0&depth=1' );
    if (is_page( )) {
      $page = $post->ID;
      if ($post->post_ID) {
        $page = $post->post_parent;
      }
      $children=wp_list_pages( 'echo=0&child_of=' . $post->ID . '&title_li=' );
      if ($children) {
        $pages = wp_list_pages ('echo=0&depth=1&child_of=' . $page . '&title_li=');
      }
    }
    //Display
    $pages_arr = explode("\n", $pages);
    for($i=0;$i<$nbpages;$i++){
    echo $pages_arr[$i];
    }

    Widget/Plugin :

    function SubPageAuto_init(){
                        register_widget("SubPageAuto_widget");
        }
    
        class SubPageAuto_widget extends WP_widget{
    
                function SubPageAuto_widget(){
                        $options = array(
                                "classname" => "Widget_autoNlimitSubPages",
                                "description" => ""
                        );
    
                        $this->WP_widget("autoNlimitSubPages","Auto & Limited SubPages",$options);
                }
    
                function widget($args,$instance)
                {
                                extract($args);
                        echo $before_widget;
                        echo $before_title.$instance["title_asp"].$after_title;
    
                        //Limit number to display
                        $nbpages = $instance["limit"];
                        //Request
                        $pages = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0&depth=1');
                        if (is_page()){
                        $page = $post->ID;
                        if ($post->post_ID){
                                $page = $post->post_parent;
                        }
                        $children=wp_list_pages('echo=0&child_of='.$post->ID.'&title_li=' );
                        if ($children){
                                $pages = wp_list_pages('echo=0&depth=1&child_of='.$page.'&title_li=');
                        }
                        }
                        //Display
                        $pages_arr = explode("\n", $pages);
                        for($i=0;$i<$nbpages;$i++){
                        echo $pages_arr[$i];
                        }
    
                        echo $after_widget;
                }
  3. kzee
    Member
    Posted 4 years ago #

    function SubPageAuto_init(){
                        register_widget("SubPageAuto_widget");
        }
    
        class SubPageAuto_widget extends WP_widget{
    
                function SubPageAuto_widget(){
                        $options = array(
                                "classname" => "Widget_autoNlimitSubPages",
                                "description" => ""
                        );
    
                        $this->WP_widget("autoNlimitSubPages","Auto & Limited SubPages",$options);
                }
    
                function widget($args,$instance)
                {
                                extract($args);
                        echo $before_widget;
                        echo $before_title.$instance["title_asp"].$after_title;
    
                        //Limit number to display
                        $nbpages = $instance["limit"];
                        //Request
                        $pages = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0&depth=1');
                        if (is_page()){
                        $page = $post->ID;
                        if ($post->post_ID){
                                $page = $post->post_parent;
                        }
                        $children=wp_list_pages('echo=0&child_of='.$post->ID.'&title_li=' );
                        if ($children){
                                $pages = wp_list_pages('echo=0&depth=1&child_of='.$page.'&title_li=');
                        }
                        }
                        //Display
                        $pages_arr = explode("\n", $pages);
                        for($i=0;$i<$nbpages;$i++){
                        echo $pages_arr[$i];
                        }
    
                        echo $after_widget;
                }

    I forgot :
    'global $post;'

    ...
    function widget($args,$instance)
                {
                                extract($args);
                        echo $before_widget;
                        echo $before_title.$instance["title_asp"].$after_title;
                        // the global request missing
                        global $post;
                        //Limit number to display
                        $nbpages = $instance["limit"];
                        //Request
    ...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.