首页 > 解决方案 > How change default parent page for custom template in wordpress?

问题描述

Input data:
The site contains only pages, no posts.
All pages are created through single templates.
There is only one template that will be used in the future to add new pages.
This template is now written in page.php that would be default template. (This could be redirected without touching page.php, but this is an extra code for this project)

All these new pages should become children of the page (for example with id = 77).
The client could manually select the parent page "name" c id = 77 each time in the drop-down list.
But this is an unnecessary action and if forget this - there will be problems with the structure in the future.

Automatic selection of the parent page should occur when you click on the "Add New"

The code probably needs to be added to function.php, it looks probably like this:

 function change_default_parent_page() {
        if(is_page_template('page.php')){
             global $post_parent = '77';
           }
         }
 add_action( 'after_setup_theme', 'change_default_parent_page' );

click on the "Add New":
click on the

like now:
like now

how should it become without using this drop-down menu:
how should it become without using this drop-down menu

标签: wordpresstemplatesparent-childdefaultparent

解决方案


有一个简单的代码可以做到这一点。只需将此波纹管代码添加到主题的functions.php中

add_filter( 'wp_insert_post_data', 'set_parnt_page_dynamically' );
function set_parnt_page_dynamically( $data )
{
    if ($data['post_type'] == 'page' )
        $data['post_parent'] = 77;
    return $data;
}

77进入我的代码是父页面id,你可以改变它。现在当用户创建一个新页面并点击发布按钮时,父页面会自动设置。试试代码,然后告诉我结果,谢谢


推荐阅读