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

时间:2018-02-10 作者:YarGnawh

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

有没有办法将另一个静态页面选项添加到阅读设置页面?是否有现有的钩子来修改它?

reading settings

还有,有没有办法在页面中这样标记所选页面?

enter image description here

Update:

正在查看/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>";
    }
}
post\\u状态似乎有挂钩,但如何设置?

还有,看看/wp-admin/options-reading.php:83

有没有办法在不修改文件的情况下修改读取设置?

1 个回复
最合适的回答,由SO网友:LWS-Mo 整理而成

我不知道是否可以直接在“Posts page”下拉字段下添加一个新字段,至少我找不到这样做的方法。

但是你可以在阅读设置页面中添加另一个下拉菜单。这将是在底部你。

要做到这一点,首先我们需要register 我们的新设置和add a new settings field 至默认WordPressreading 页码:

/**
 * Register and define the settings
 */
add_action(\'admin_init\', \'prfx_admin_init\');
function prfx_admin_init(){

    // register our setting
    $args = array(
        \'type\' => \'string\', 
        \'sanitize_callback\' => \'sanitize_text_field\',
        \'default\' => NULL,
    );
    register_setting( 
        \'reading\', // option group "reading", default WP group
        \'my_project_archive_page\', // option name
        $args 
    );

    // add our new setting
    add_settings_field(
        \'my_project_archive_page\', // ID
        __(\'Project Archive Page\', \'textdomain\'), // Title
        \'prfx_setting_callback_function\', // Callback
        \'reading\', // page
        \'default\', // section
        array( \'label_for\' => \'my_project_archive_page\' )
    );
}
现在,我们可以为自定义字段创建回调函数,它将保存HTML标记:

/**
 * Custom field callback
 */
function prfx_setting_callback_function($args){
    // get saved project page ID
    $project_page_id = get_option(\'my_project_archive_page\');

    // get all pages
    $args = array(
        \'posts_per_page\'   => -1,
        \'orderby\'          => \'name\',
        \'order\'            => \'ASC\',
        \'post_type\'        => \'page\',
    );
    $items = get_posts( $args );

    echo \'<select id="my_project_archive_page" name="my_project_archive_page">\';
    // empty option as default
    echo \'<option value="0">\'.__(\'— Select —\', \'wordpress\').\'</option>\';

    // foreach page we create an option element, with the post-ID as value
    foreach($items as $item) {

        // add selected to the option if value is the same as $project_page_id
        $selected = ($project_page_id == $item->ID) ? \'selected="selected"\' : \'\';

        echo \'<option value="\'.$item->ID.\'" \'.$selected.\'>\'.$item->post_title.\'</option>\';
    }

    echo \'</select>\';
}
在此之后,您现在有了一个新的下拉字段Settings > Reading.
该字段填充了所有页面,您可以选择一个页面并保存设置。

所以现在你想mark “管理”中页面列表上的此选定页面。这些标记称为state/s. WordPress使用过滤器将这些文本添加到名为display_post_states.

/**
 * Add custom state to post/page list
 */
add_filter(\'display_post_states\', \'prfx_add_custom_post_states\');

function prfx_add_custom_post_states($states) {
    global $post;

    // get saved project page ID
    $project_page_id = get_option(\'my_project_archive_page\');

    // add our custom state after the post title only,
    // if post-type is "page",
    // "$post->ID" matches the "$project_page_id",
    // and "$project_page_id" is not "0"
    if( \'page\' == get_post_type($post->ID) && $post->ID == $project_page_id && $project_page_id != \'0\') {
        $states[] = __(\'My state\', \'textdomain\');
    }

    return $states;
}

结束