wordpress - 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' );
解决方案
有一个简单的代码可以做到这一点。只需将此波纹管代码添加到主题的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,你可以改变它。现在当用户创建一个新页面并点击发布按钮时,父页面会自动设置。试试代码,然后告诉我结果,谢谢
推荐阅读
- java - 在方向图中从 JAVA 添加具有属性的类顶点以及另一个类顶点的列表等
- unity3d - 统一网络。transform.setparent() 在 clinet 端不起作用
- excel - 无法保存Openpyxl python3修改的现有excel文档
- sql - 如何通过在任何插入的两个值之间改组来将列上的两个值指定为 DEFAULT
- delphi - delphi中的OnDrawCell过程访问冲突
- java - 如何将图像保存到文件夹并将链接保存到 mySQL 数据库
- bash - 在 bash 中编写一个可移植的通用确认函数——你能改进它吗?
- angular - Css 不适用于 Angular 6
- python - 从python调用bash命令时“致命:参数太多”,直接在git bash中使用相同的命令没有错误
- python - 当我尝试通过 python 发送请求时,$ 中的错误读取不是有效的 json 值