激活插件WordPress时创建页面

sau*_*rox -1 wordpress plugins

有人请帮忙!我有一个在后端创建post_type页面的插件。该插件正在创建所需的页面,但是问题是每当我尝试查看页面列表时,它都会显示“找不到页面”消息。萤幕撷取画面:http : //prnt.sc/azalub

我在此处创建所需页面的代码:

$new_page = array('post_title'    => $title,
                  'post_content'  => '['.$shortcode.']',
                  'post_status'   => 'publish',
                  'post_type'     => 'page'
                );
$post_id = wp_insert_post( $new_page );
Run Code Online (Sandbox Code Playgroud)

Man*_*har 6

为此,您需要向插件激活注册。
请参见下面的代码示例:

function add_my_custom_page() {
    // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( 'My Custom Page' ),
      'post_content'  => 'My custom page content',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'page',
    );

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

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

在此处输入图片说明


sau*_*rox 1

在创建自定义帖子类型时,我在插件中的自定义帖子类型之一上将“query_var”设置为“true”。将其设置为“假”只会让一切都变得很好。