WordPress.org

Forums

Better customization for WP List Pages (2 posts)

  1. kath
    Member
    Posted 5 years ago #

    This is something that has driven me crazy about WordPress for ages. WP List Pages is pretty inflexible for HTML. So if I want to add descriptions to my WP list pages, for example, I have to actually go intp wordpress core, and since I don't want to edit WordPress core, I copy wp_list_pages and several filters and classes and other PHP. When there's really basically one thing I want to change -- the start and end variables for the elements and levels. I also did one modification where I wanted to use different start/end level HTML for level 0 (depth=1) and for any other levels.

    This means that I need to update this code pretty often and then redo all of my custom references to new functions because wp_list_pages does get updated. IMO this is a pretty reasonable thing to wish to edit, and it would be great to have something like the shortcodes API to use to make custom html for WP list pages -- I guess in an additional arg added to wp_list_pages and then maybe add it via apply_filters or something. This way one could make several additional HTML options for wp_list_pages for different specific uses.

    Oh, by the way, my use case for WP List Pages as opposed to get_posts or something (which I could customize completely) is the tree-walking goodness done by WP_List_pages. If this is easier than I think it is let me know but it seems alarming to me to write a php function based on get_posts that could go down a very large number of levels. I don't want to have to worry about running out of levels if down the line the site needs 6 and I only wrote the menu with 5. And WP List Pages keeps track of level number already and where it is in the tree.

    I hope no one has posted this question. I took a look at the tags and didn't see anything.

  2. kath
    Member
    Posted 5 years ago #

    Er, of course when you look more you find the answer. Think this was added since the last time I did this but it's MUCH BETTER: http://bugssite.org/blog/2009/12/08/wordpress-custom-walker-tutorial/

    So hopefully this will make this a little more findable.

    This is how I have it implemented:

    class Walker_Page_Greenfield extends Walker_Page {
    	/**
    	 * @see Walker::start_lvl()
    	 * @since 2.1.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param int $depth Depth of page. Used for padding.
    	 */
    	function start_lvl(&$output, $depth) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "\n$indent<div class=\"menuwide\"><ul>\n";
    	}
    
    	/**
    	 * @see Walker::end_lvl()
    	 * @since 2.1.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param int $depth Depth of page. Used for padding.
    	 */
    	function end_lvl(&$output, $depth) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent</ul></div>\n";
    	}
    
    	}
    ?>

    And

    `<?php $walker_pages = new Walker_Page_Greenfield; wp_list_pages(array('walker' => $walker_pages, 'title_li' => '', 'child_of' => '6', 'exclude' => '218')); ?>'

Topic Closed

This topic has been closed to new replies.

About this Topic