Last active
October 17, 2017 04:37
-
-
Save tuanpht/2b7cbef37915a613858119949f1a6b17 to your computer and use it in GitHub Desktop.
Custom wp_dropdown_pages walker class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Custom_Walker_PageDropdown extends Walker_PageDropdown { | |
/** | |
* Starts the element output. | |
* @param array|string $args { | |
* | |
* @type int|string|callable $selected Value of the option that should be selected. | |
* Default 0. | |
* Or a callback that return true/false. | |
* @type string|callable $value_field Post field or a callback used to populate | |
* the 'value' attribute of the option elements. | |
* Default 'ID'. | |
* | |
* @see Walker_PageDropdown | |
*/ | |
public function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) { | |
$pad = str_repeat(' ', $depth * 3); | |
// Get value for <option> | |
$value = $page->ID; | |
if ( isset( $args['value_field'] ) ) { | |
$value_field = $args['value_field']; | |
if ( is_callable( $value_field ) ) { | |
$value = call_user_func( $value_field, $page ); | |
} elseif ( isset( $page->{$value_field} ) ) { | |
$value = $page->{$value_field}; | |
} | |
} | |
$output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $value ) . "\""; | |
// Check if <option> is selected | |
if ( isset( $args['selected'] ) ) { | |
$selected = $args['selected']; | |
$is_selected = false; | |
if ( is_callable( $selected ) ) { | |
$is_selected = call_user_func( $selected, $page ); | |
} else { | |
$is_selected = ( $value == $selected ); | |
} | |
if ( $is_selected ) { | |
$output .= ' selected="selected"'; | |
} | |
} | |
$output .= '>'; | |
$title = $page->post_title; | |
if ( '' === $title ) { | |
/* translators: %d: ID of a post */ | |
$title = sprintf( __( '#%d (no title)' ), $page->ID ); | |
} | |
/** | |
* Filters the page title when creating an HTML drop-down list of pages. | |
* | |
* @since 3.1.0 | |
* | |
* @param string $title Page title. | |
* @param object $page Page data object. | |
*/ | |
$title = apply_filters( 'list_pages', $title, $page ); | |
$output .= $pad . esc_html( $title ); | |
$output .= "</option>\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wp_dropdown_pages( [ | |
'value_field' => function ( $page ) { | |
return $page->ID . ' - ' . $page->post_name; | |
}, | |
'selected' => function ( $page ) { | |
return $page->post_name == 'blog'; // get_data...() | |
}, | |
'walker' => new Custom_Walker_PageDropdown, | |
] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment