• Is there any way to append a custom field value after the title of a page in the wp_dropdown_pages function?

    This is what I have now:

    <form action="<?php bloginfo('url'); ?>" method="get">
    <?php $select = wp_dropdown_pages( array('post_type' => 'page', 'show_option_none' => '— Select —', 'child_of' => 6, 'sort_column' => 'post_title') ); echo str_replace('<select ', '<select onchange="this.form.submit()" ', $select); ?>
    </form>
Viewing 1 replies (of 1 total)
  • There is a filter, list_pages, that lets you modify the title of a page when used in a dropdown.

    If you wanted to append a field to the end of the title, you’d hook into list pages and conditionally add one if it exists, like so:

    function my_list_pages_custom_field( $title, $page ) {
    	$custom_field = get_post_meta( $page->ID, 'custom_field', true );
    
    	if ( $custom_field) {
    		$title = $title . ' - ' . $custom_field;
    	}
    
    	return $title;
    }
    add_filter( 'list_pages', 'my_list_pages_custom_field', 10, 2 );
    

    The only thing is that this will apply to all page dropdowns, such as in the admin. If you want to only apply it for a specific dropdown you would need to hook it right before creating the dropdown, then unhooking right after:

    add_filter( 'list_pages', 'my_list_pages_custom_field', 10, 2 );
    wp_dropdown_pages();
    remove_filter( 'list_pages', 'my_list_pages_custom_field', 10 );
    
Viewing 1 replies (of 1 total)
  • The topic ‘Add custom field value after title in wp_dropdown_pages function?’ is closed to new replies.