Иерархия постов в WordPress (вложенность постов)
В самом ядре WordPress есть функция
function create_initial_post_types() {
register_post_type(
'post',
array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
),
'public' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_position' => 5,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
'show_in_rest' => true,
'rest_base' => 'posts',
'rest_controller_class' => 'WP_REST_Posts_Controller',
)
);
Мне нужно сделать поддержку иерархии для записей, если изменить параметр hierarchical на true и в supports добавить 'page-attributes' то все работает. Но при обновлении такие настройки слетят. Как можно сделать тоже самое только что настройки не слетели при обновлении wordpress?
Ответы (1 шт):
Автор решения: igorauscas
→ Ссылка
В файл functions.php вашей темы нужно добавить
// Add page attributes for posts
add_filter( 'register_post_type_args', 'change_register_post_type_args', 10, 2 );
function change_register_post_type_args ( $args, $post_type ) {
if ( 'post' === $post_type ) {
$args['hierarchical'] = true;
}
return $args;
}
add_post_type_support('post', array('page-attributes') );