WordPress.org

Ready to get started?Download WordPress

Forums

Get Sub Pages but custom HTML (10 posts)

  1. paulanthony
    Member
    Posted 5 years ago #

    Guys - What I'm trying to do is generate Page titles at the root, and then list subpages which are linked off to actual pages. Wp-list-pages doesn't seem to cater for my needs. The below structure illustrates better.

    -Loop all pages in the site
    <!--notice no root (homepage)-->
    <h2>Page 1</h2>
    <!--notice page one is a pseudo page-->
      <div class="subpagesofPage1">
      linktosubpage1 | linktosubpage2 | linksubpage3
      </div>
    <h2>Page 2</h2>
      <div class="subpagesofPage2">
      linktosubpage4 | linktosubpage5 | linksubpage6
      </div>
    <!--Dont show page 3 <h2>Page 3</h2>-->
  2. paulanthony
    Member
    Posted 5 years ago #

    <div>
    
    	<?php
    
    $children = wp_list_pages("title_li=&echo=0");
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>
    </div>

    Or in other words - the above code, without the parents being linked.

  3. Joost de Valk
    Member
    Posted 5 years ago #

    Check here to see how wp_list_pages works. You could just copy the function and make it do what you want :)

  4. paulanthony
    Member
    Posted 5 years ago #

    wp_list_pages relies heavily on get_pages(), or seems to. I'll need to modify / copy paste it as well, which seems like an ugly solution to a relatively small problem.

  5. paulanthony
    Member
    Posted 5 years ago #

  6. paulanthony
    Member
    Posted 5 years ago #

  7. paulanthony
    Member
    Posted 5 years ago #

    Alright folks I found the answer. You have to override the Walker class. you can find this in classes.php

    class Walker_Webdistortion extends Walker {
    	var $tree_type = 'page';
    	var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
    
    	function start_lvl(&$output, $depth) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "\n$indent<ul class='subpage'>\n";
    	}
    
    	function end_lvl(&$output, $depth) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent</ul>\n";
    	}
    
    	function start_el(&$output, $page, $depth, $current_page, $args) {
    		if ( $depth )
    			$indent = str_repeat("\t", $depth);
    		else
    			$indent = '';
    
    		extract($args, EXTR_SKIP);
    		$css_class = 'page_item page-item-'.$page->ID;
    		if ( !empty($current_page) ) {
    			$_current_page = get_page( $current_page );
    			if ( in_array($page->ID, (array) $_current_page->ancestors) )
    				$css_class .= ' current_page_ancestor';
    			if ( $page->ID == $current_page )
    				$css_class .= ' current_page_item';
    			elseif ( $_current_page && $page->ID == $_current_page->post_parent )
    				$css_class .= ' current_page_parent';
    		}
    		//get custom field toplevel=1 then dont link
    
      $custom_fields = get_post_custom($page->ID);
      $my_custom_field = $custom_fields['toplevel'];
    
    if($my_custom_field[0]==1){
    $output .= '<div>'. attribute_escape(apply_filters('the_title', $page->post_title)) .'</div>';
    }else{
    
    		$output .=  $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('the_title', $page->post_title)) . '">' . apply_filters('the_title', $page->post_title) . '</a>';
    
    }
    
    		if ( !empty($show_date) ) {
    			if ( 'modified' == $show_date )
    				$time = $page->post_modified;
    			else
    				$time = $page->post_date;
    
    			$output .= " " . mysql2date($date_format, $time);
    		}
    	}
    
    	function end_el(&$output, $page, $depth) {
    		$output .= "</li>\n";
    	}
    
    }

    Then create a copy of the wp_list_pages function. And wack that into your page.

    <?php
    function my_wp_list_pages($args = '') {
          $defaults = array(
             'depth' => 0, 'show_date' => '',
              'date_format' => get_option('date_format'),
            'child_of' => 0, 'exclude' => '',
              'title_li' => '',
    		  'echo' => 1,
            'authors' => '', 'sort_column' => 'menu_order, post_title',
           'link_before' => '', 'link_after' => ''
          );
    
         $r = wp_parse_args( $args, $defaults );
          extract( $r, EXTR_SKIP );
    
         $output = '';
          $current_page = 0;
    
          // sanitize, mostly to keep spaces out
         $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
    
         // Allow plugins to filter an array of excluded pages
        $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
    
          // Query pages.
        $r['hierarchical'] = 1;
         $pages = get_pages($r);
    
         if ( !empty($pages) ) {
             if ( $r['title_li'] )
                 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
    
            global $wp_query;
            if ( is_page() || $wp_query->is_posts_page )
                 $current_page = $wp_query->get_queried_object_id();
             $output .=  walk_page_webdistortion($pages, $r['depth'], $current_page, $r);
    
             if ( $r['title_li'] )
                  $output .= '</ul></li>';
          }
    
          //$output = apply_filters('wp_list_pages', $output);
    
         if ( $r['echo'] )
             echo $output;
        else
              return $output;
    }
    echo(my_wp_list_pages());
    
    ?>

    Notice $output .= walk_page_webdistortion

    Finally tie up your class. In post_template.

    function walk_page_webdistortion(){
    	$walker = new Walker_Webdistortion;
    	$args = func_get_args();
    	return call_user_func_array(array(&$walker, 'walk'), $args);
    }

    You'll also need to create custom fields for each page which is a pseduopage. toplevel=1 is the variable I checked for in the custom Walker class.

    disclaimer. I'm not a PHP coder, so this stuff was relatively alien to me. There is probably a much tidier cleaner way of doing this, but please WP developers lets get the html out of the codebase, or if it must go into it, get it configurable for each function.

  8. syncbox
    Member
    Posted 4 years ago #

    Yes, I agree wholeheartedly. I'd like the option to wrap a div around the nested list generated for child pages... so that I can tap into some of my favorite javascript menu scripts --

    Anyone out there who can tell me how to modify start_el to output a div around the nested ul? only at the child level?

    ??

  9. syncbox
    Member
    Posted 4 years ago #

    Yes, I agree wholeheartedly. I'd like the option to wrap a div around the nested list generated for child pages... so that I can tap into some of my favorite javascript menu scripts --

    Anyone out there who can tell me how to modify start_el to output a div around the nested ul? only at the child level?

    <ul>
    <li><a href="#">parent #1</a>
    <div>
    <ul>
    <li><a href="#">child 1-a</a></li>
    <li><a href="#">child 1-b</a></li>
    <li><a href="#">child 1-c</a></li>
    <li><a href="#">child 1-d</a></li>
    <li><a href="#">child 1-e</a></li>
    </ul>
    </div>
    </li>
    <li><a href="#">parent #2</a>
    <div>
    <ul>
    <li><a href="#">child 2-a</a></li>
    <li><a href="#">child 2-b</a></li>
    <li><a href="#">child 2-c</a></li>
    <li><a href="#">child 2-d</a></li>
    </ul>
    </div>
    </li>
    <li><a href="#">parent #31</a>
    <div>
    <ul>
    <li><a href="#">child 3-a</a></li>
    <li><a href="#">child 3-b</a></li>
    <li><a href="#">child 3-c</a></li>
    </ul>
    </div>
    </li>
    </ul>

    ??

  10. syncbox
    Member
    Posted 4 years ago #

    Seems like this would be a great idea for a plugin -- that would allow you to wrap child lists with your choice of markup... I'm not a php programmer or plugin writer, but... I can't tell you how many times I wished I had this functionality so that I don't have to "settle" for ho-hum menus...

    I'd even donate generously to someone who created it!

Topic Closed

This topic has been closed to new replies.

About this Topic