我不知道是否可以直接在“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;
}