Add Static Page to Reading Settings for Custom Post Type Add Static Page to Reading Settings for Custom Post Type wordpress wordpress

Add Static Page to Reading Settings for Custom Post Type


Is there a way to add another static page option to the reading settings page?

Yes.

Code:

/** * Adds a custom field: "Projects page"; on the "Settings > Reading" page. */add_action( 'admin_init', function () {    $id = 'page_for_projects';    // add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() )    add_settings_field( $id, 'Projects page:', 'settings_field_page_for_projects', 'reading', 'default', array(        'label_for' => 'field-' . $id, // A unique ID for the field. Optional.        'class'     => 'row-' . $id,   // A unique class for the TR. Optional.    ) );} );/** * Renders the custom "Projects page" field. * * @param array $args */function settings_field_page_for_projects( $args ) {    $id = 'page_for_projects';    wp_dropdown_pages( array(        'name'              => $id,        'show_option_none'  => '— Select —',        'option_none_value' => '0',        'selected'          => get_option( $id ),    ) );}/** * Adds page_for_projects to the white-listed options, which are automatically * updated by WordPress. * * @param array $options */add_filter( 'whitelist_options', function ( $options ) {    $options['reading'][] = 'page_for_projects';    return $options;} );

Preview:

enter image description here

Also, is there a way to mark the selected page like this in Pages?

Yes.

Code:

/** * Filters the post states on the "Pages" edit page. Displays "Projects Page" * after the post/page title, if the current page is the Projects static page. * * @param array $states * @param WP_Post $post */add_filter( 'display_post_states', function ( $states, $post ) {    if ( intval( get_option( 'page_for_projects' ) ) === $post->ID ) {        $states['page_for_projects'] = __( 'Projects Page' );    }    return $states;}, 10, 2 );

Preview:

enter image description here

And if you must or need to, you can use JavaScript/jQuery to move the "Projects page" field to below the "Posts page" in the "Your homepage displays" column.