WordPress 关于我页面,安装新主题时复制

发布于 2024-12-25 14:39:22 字数 144 浏览 6 评论 0原文

我想要做的是安装一个模板,该模板会自动创建一个页面,就像第一次安装 WordPress 时创建的 abotu me 页面一样。我该怎么做?安装新主题时如何运行SQL语句?由于页面存储在数据库中,我可以在安装主题时以这种方式上传页面,但我不知道如何上传。

此致

What I want to do is install a template, and that template automaticly create a page, like the abotu me page is created when you first install wordpress. how can I do this? How do you run an SQL statment when you install a new theme? since pages are stored in the database I could just upload teh page this way when installing the theme but I have no ideia how.

Best Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溇涏 2025-01-01 14:39:22

JCHASE,我同意 wp_insert_post 部分,我唯一建议的是使用操作钩子而不是 $_GET。使用 add_action( 'after_setup_theme', 'my_initiation_function') 将是一个更好的选择

JCHASE, I agree with wp_insert_post part, the only thing that I would suggest is using an action hook instead of $_GET. Using add_action( 'after_setup_theme', 'my_initiation_function') would be a better option

记忆消瘦 2025-01-01 14:39:22

我上面的回答是为了添加帖子。下面的答案是添加一个页面(它添加主题激活的页面):

if (isset($_GET['activated']) && is_admin()){
    $new_page_title = 'This is the page title';
    $new_page_content = 'This is the page content';
    $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
    //don't change the code bellow, unless you know what you're doing
    $page_check = get_page_by_title($new_page_title);
    $new_page = array(
        'post_type' => 'page',
        'post_title' => $new_page_title,
        'post_content' => $new_page_content,
        'post_status' => 'publish',
        'post_author' => 1,
    );
    if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);
        if(!empty($new_page_template)){
            update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
        }
    }
}

My above answer is for adding posts. The below answer is to add a page (it adds the page on theme activation):

if (isset($_GET['activated']) && is_admin()){
    $new_page_title = 'This is the page title';
    $new_page_content = 'This is the page content';
    $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
    //don't change the code bellow, unless you know what you're doing
    $page_check = get_page_by_title($new_page_title);
    $new_page = array(
        'post_type' => 'page',
        'post_title' => $new_page_title,
        'post_content' => $new_page_content,
        'post_status' => 'publish',
        'post_author' => 1,
    );
    if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);
        if(!empty($new_page_template)){
            update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
        }
    }
}
心凉怎暖 2025-01-01 14:39:22

这是对帖子执行此操作的方法:

global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

请参阅下面的页面。请参阅此页面以获取参考。

This is how to do it for posts:

global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

See below for pages. See this page for the reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文