• edw!n

    (@eslothouber)


    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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter edw!n

    (@eslothouber)

    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?

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter edw!n

    (@eslothouber)

    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>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_dropdown_pages() with parents disabled’ is closed to new replies.