WordPress.org

Ready to get started?Download WordPress

Forums

wp_dropdown_pages() with parents disabled (5 posts)

  1. edw!n
    Member
    Posted 1 year ago #

    I wnat to use wp_dropdown_pages() the get a select with all pages within the site. But for all pages that have children, I'd like the <option> to be disabled, because these pages don't have any content. They are there just for hierachial reasons.

    So

    <select >
    	<option class="level-0" value="2">Home</option>
    	<option class="level-0" value="8">Praktisch</option>
    	<option class="level-1" value="53">&nbsp;&nbsp;&nbsp;Contact & Route</option>
    	<option class="level-1" value="55">  Mission</option>
    	<option class="level-1" value="57">  Who is who</option>
    	<option class="level-0" value="12">Schrijfcoaching</option>
    	<option class="level-1" value="63">  Schrijfcoaching</option>
    	<option class="level-1" value="65">  Manuscriptbeoordeling</option>
    </select>

    should be

    <select >
    	<option class="level-0" value="2" disabled>Home</option>
    	<option class="level-0" value="8">Praktisch</option>
    	<option class="level-1" value="53">&nbsp;&nbsp;&nbsp;Contact & Route</option>
    	<option class="level-1" value="55">  Mission</option>
    	<option class="level-1" value="57">  Who is who</option>
    	<option class="level-0" value="12" disabled>Schrijfcoaching</option>
    	<option class="level-1" value="63">  Schrijfcoaching</option>
    	<option class="level-1" value="65">  Manuscriptbeoordeling</option>
    </select>

    Is there a way to do this?

  2. bcworkz
    Member
    Posted 1 year ago #

    Sort of, but it's not ideal. You can suppress options for children altogether, but altering only those attributes is not a built in capability. You could hook into 'wp_dropdown_pages' to alter the actual HTML output. Your script would have to search for unique substrings for which to add the disabled attribute. If all your values are unique, this could be a viable approach.

  3. edw!n
    Member
    Posted 1 year ago #

    Thanks bcworkz,

    I think I better build a wp_dropdown_pages() myself, that will probably be faster than to filter out stuff that I don't want.

    Strange though that no one else run into this before?

  4. bcworkz
    Member
    Posted 1 year ago #

    I think you're right, doing your own function is the best approach. It would be more efficient because you can leave out much of the code intended to provide flexibility to all comers.

    These sort of functions cannot be all things to all people, someone decided that simply suppressing the child options entirely would be adequate for most people. For those that it is not adequate, they are free to roll their own, it's not a huge impediment.

  5. edw!n
    Member
    Posted 1 year ago #

    And if someone is looking for something simular: this is what I came up with (and some searching on the web :-)

    function pages_dropdown($parentId, $lvl, $currentPageId) {
    	$args=array('child_of' => $parentId, 'parent' => $parentId, 'sort_column' => 'menu_order');
    	$pages = get_pages($args);
    	if ($pages) {
    		$lvl ++;
    		foreach ($pages as $page) {
    			$selected = ( $currentPageId == $page->ID) ? 'selected' : '';
    			$disabled = ( get_pages('child_of='.$page->ID.'&parent='.$page->ID) ) ? 'disabled' : '';
    			echo "<option value='" . $page->ID . "' $disabled $selected >" . str_repeat(" ", $lvl*3) . $page->post_title  . "</option>";
    			pages_dropdown($page->ID, $lvl, $currentPageId);
    		}
    	}
    }
    
    // and somewhere in your template:
    <select>
    <?php pages_dropdown(0, 0, get_the_ID() ); ?>
    </select>

Topic Closed

This topic has been closed to new replies.

About this Topic