我创建了自定义帖子类型,我想让用户选择存档页面的静态页面。例如,在Post Page下。我想添加一个项目页面:[下拉列表]
有没有办法在阅读设置页面添加另一个静态页面选项?是否有现有的钩子来修改它?
另外,有没有办法在Pages中标记所选页面?
更新
查看/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_state有钩子,但是你如何设置呢?
另外,查看/wp-admin/options-reading.php:83
有没有办法在不实际修改文件的情况下修改阅读设置?
答案 0 :(得分:4)
有没有办法在阅读中添加另一个静态页面选项 设置页面?
是
代码:
/**
* 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;
} );
预览:
另外,有没有办法在Pages中标记所选页面?
是
代码:
/**
* 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 );
预览:
如果您必须或需要,您可以使用JavaScript / jQuery移动&#34; 项目页面&#34;字段以下&#34; 帖子页面&#34;在&#34; 您的主页显示&#34;列。