将静态页面添加到自定义帖子类型的阅读设置

Yar*_*awh 2 wordpress

我创建了一个自定义帖子类型,我想让用户为存档页面选择静态页面。例如,在“帖子”页面下。我想添加一个“项目”页面:[下拉菜单]

有没有一种方法可以向阅读设置页面添加另一个静态页面选项?是否有现有的钩子可以对其进行修改?

阅读设置

另外,是否可以在“页面”中像这样标记所选页面?

在此处输入图片说明

更新:

看着 /wp/wp-admin/includes/template.php:1768

/**
 * Filters the default post display states used in the posts list table.
 *
 * @since 2.8.0
 * @since 3.6.0 Added the `$post` parameter.
 *
 * @param array   $post_states An array of post display states.
 * @param WP_Post $post        The current post object.
 */
$post_states = apply_filters( 'display_post_states', $post_states, $post );

if ( ! empty($post_states) ) {
    $state_count = count($post_states);
    $i = 0;
    echo ' — ';
    foreach ( $post_states as $state ) {
        ++$i;
        ( $i == $state_count ) ? $sep = '' : $sep = ', ';
        echo "<span class='post-state'>$state$sep</span>";
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎有对post_state的钩子,但是如何设置它?

另外,看着 /wp-admin/options-reading.php:83

是否可以在不实际修改文件的情况下修改“阅读”设置?

Sal*_* CJ 5

有没有一种方法可以向阅读设置页面添加另一个静态页面选项?

是。

码:

/**
 * 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'  => '&mdash; Select &mdash;',
        '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;
} );
Run Code Online (Sandbox Code Playgroud)

预习:

在此处输入图片说明

另外,是否可以在“页面”中像这样标记所选页面?

是。

码:

/**
 * 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 );
Run Code Online (Sandbox Code Playgroud)

预习:

在此处输入图片说明

并且,如果必须或需要,您可以使用JavaScript / jQuery将“ 项目页面 ”字段移到“ 您的主页显示 ”列中的“ 帖子页面 ” 下方。