如何以编程方式创建新的wordpress页面

Spp*_*idy 3 php wordpress

我在wordpress中创建一个插件.我无法找到使用插件创建新wordpress页面的方法.我想在用户激活插件时在wordpress的前端创建一个新页面.

son*_*ade 13

借助上述代码,您可以创建动态页面。首先,我们需要检查我们要创建的帖子是否可用。如果存在,您不需要创建另一个,您可以编辑页面的内容。但是,如果您更改了页面的标题,则会创建新页面。在这里,我在页面标题的帮助下创建了一个页面。

$check_page_exist = get_page_by_title('title_of_the_page', 'OBJECT', 'page');
// Check if the page already exists
if(empty($check_page_exist)) {
    $page_id = wp_insert_post(
        array(
        'comment_status' => 'close',
        'ping_status'    => 'close',
        'post_author'    => 1,
        'post_title'     => ucwords('title_of_the_page'),
        'post_name'      => strtolower(str_replace(' ', '-', trim('title_of_the_page'))),
        'post_status'    => 'publish',
        'post_content'   => 'Content of the page',
        'post_type'      => 'page',
        'post_parent'    => 'id_of_the_parent_page_if_it_available'
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

  • 替换: ` 'post_name' => strtolower(str_replace(' ', '-', trim('title_of_the_page'))), ` with: ` 'post_name' => sanitize_title('title_of_the_page'), ` <-- 使用 Wordpress函数 https://developer.wordpress.org/reference/functions/sanitize_title/ (3认同)

Igo*_*ych 7

像这样的东西

function some_function()
{
   $post_details = array(
  'post_title'    => 'Page title',
  'post_content'  => 'Content of your page',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_type' => 'page'
   );
   wp_insert_post( $post_details );
}

register_activation_hook(__FILE__, 'some_function');
Run Code Online (Sandbox Code Playgroud)

请参阅此处了解其他可能的参数.

  • 像这样 `$post_details = array( 'ID' => $id, 'post_content' => '<p>这是一个简单的段落。</p>', ); wp_update_post( $post_details );` 你可以像这样从 wp_insert_post 获取新创建的页面的 ID `$id=wp_insert_post( $post_details );` (2认同)

小智 6

一个方便的辅助函数来创建一堆页面:

function create_page($title_of_the_page,$content,$parent_id = NULL ) 
{
    $objPage = get_page_by_title($title_of_the_page, 'OBJECT', 'page');
    if( ! empty( $objPage ) )
    {
        echo "Page already exists:" . $title_of_the_page . "<br/>";
        return $objPage->ID;
    }
    
    $page_id = wp_insert_post(
            array(
            'comment_status' => 'close',
            'ping_status'    => 'close',
            'post_author'    => 1,
            'post_title'     => ucwords($title_of_the_page),
            'post_name'      => strtolower(str_replace(' ', '-', trim($title_of_the_page))),
            'post_status'    => 'publish',
            'post_content'   => $content,
            'post_type'      => 'page',
            'post_parent'    =>  $parent_id //'id_of_the_parent_page_if_it_available'
            )
        );
    echo "Created page_id=". $page_id." for page '".$title_of_the_page. "'<br/>";
    return $page_id;
}

create_page( 'How it works', 'This is how it works');
create_page( 'Contact Us', 'The contact us page');
create_page( 'About Us', 'The about us page');
create_page( 'Team', 'The team page');
$pid = create_page( 'Sample Page', 'This is sample page');
create_page( 'Sample SubPage 1', 'This is sample SubPage 1',$pid);
create_page( 'Sample SubPage 2', 'This is sample SubPage 2',$pid);

Run Code Online (Sandbox Code Playgroud)


Ghu*_*Ali 5

这样就可以添加页面了

// Create post object
 $my_post = array(
   'post_type'     => 'page',
   'post_title'    => 'My post',
   'post_content'  => 'This is my post.',
   'post_status'   => 'publish',
   'post_author'   => 1
 );

 // Insert the post into the database
wp_insert_post( $my_post );
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此https://codex.wordpress.org/Function_Reference/wp_insert_post

  • 这将插入一篇文章,而不是一个页面。您需要将`post_type`指定为“page”。 (7认同)