简体   繁体   English

WordPress的。 如何在PHP中创建父页面给父页面

[英]Wordpress. How to give parent page to a page create dinamiccaly in PHP

I'm creating a little script to create dinamically a page if this not exist, but I've a problem. 我正在创建一个小脚本以动态创建页面(如果该页面不存在),但是我遇到了问题。 I want to give a parent page to the page that I going to create. 我要为要创建的页面提供父页面。

In few words something like: 用几句话说:

if the page is created give it a Parent Page. 如果页面已创建,则为其提供父页面。

The Output that i want is: 我想要的输出是:

http://exemple.com/ParentPage/ChildPage http://exemple.com/ParentPage/ChildPage

and in the backend show automatically the Parent Page in the Page attributes instead of "(no parent)" 并在后端自动在“页面”属性中显示“父页面”,而不是“(无父)”

function check_pages_live(){
    if(get_page_by_title('Page Title') == NULL ) {
        create_pages_fly('Page Title');
    }

}
add_action('init','check_pages_live');

function create_pages_fly($pageName) {

    $parentPost = get_post( 2167 );

    $createPage = array(

      'post_title'    => $pageName,
      'post_content'  => 'Some Content',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'page',
      'post_parent'   => $parentPost,
      'post_name'     => $pageName
    );

    // Insert the post into the database
    wp_insert_post( $createPage );
}

I've tried to create a class with get_post and the ID of the Parent Page; 我试图用get_post和父页面的ID创建一个类。 I've added that to the function, but nothing work. 我已经将其添加到函数中,但是没有任何效果。

Someone can help me please? 有人可以帮我吗?

Thank a lot 非常感谢

Looking at the docs , 'post_parent' should be an integer. 查看文档 ,“ post_parent”应为整数。 Setting it to 'post_parent' => 2167 should do the trick. 将其设置为“ post_parent” => 2167应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM