一种在主题安装中自动安装页面的方法?


8

我正在尝试建立一个需要包含特定页面的主题。安装主题后,是否可以自动创建页面并将其分配给页面模板?


1
我敢肯定,这里已经问过完全相同的问题。只是搜索更多。
史蒂文

我什么也找不到,所以才问。如果您知道某个职位,请分享。
JonnyPlow

Answers:



3

我遇到类似的情况,我需要添加有关主题激活的页面并将其自动设置为主页。

这是我的做法:

add_action('after_setup_theme', 'mytheme_setup');

function mytheme_setup(){

 if(get_option('page_on_front')=='0' && get_option('show_on_front')=='posts'){
        // Create homepage
        $homepage = array(
            'post_type'    => 'page',
            'post_title'    => 'Home',
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1
        ); 
        // Insert the post into the database
        $homepage_id =  wp_insert_post( $homepage );
        // set this page as homepage
        update_option('show_on_front', 'page');
        update_option('page_on_front', $homepage_id);
    }

}

希望这对某人有帮助。

更新:

add_action('after_setup_theme', 'mytheme_setup');

function mytheme_setup(){

 if(get_option('page_on_front')=='0' && get_option('show_on_front')=='posts'){
        // Create homepage
        $homepage = array(
            'post_type'    => 'page',
            'post_title'    => 'Home',
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1
        ); 
        // Insert the post into the database
        $homepage_id =  wp_insert_post( $homepage );
        //set the page template 
        //assuming you have defined template on your-template-filename.php
        update_post_meta($homepage_id, '_wp_page_template', 'your-template-filename.php');
    }

}

谢谢 Maruti Mohanty


尽管您已经提到了该解决方案的详细信息,但这并不是用户所需要的。但是,如果您还可以在新帖子中添加如何添加/指定页面模板的方法,则可能会给用户提供工作的见解
Maruti Mohanty 2014年

1
编辑。感谢您为我指明正确的方向。
Dharma Poudel 2014年

1

这是不好的做法。主题是用于呈现的,它们是视觉的,它们不应该指示实际的内容。如果他们这样做,那么您做错了,您的代码作为插件会更好。

但是,如果您的主题具有“预期”的内容安排,则不应自动创建页面/内容,也不应完全与用户内容混为一谈,否则可能会破坏其他功能。

相反,允许用户选择应使用的页面,以相同的方式,设置可以让您选择静态页面而不是最新的帖子,以及Woocommerce或Jigoshop之类的插件如何让您选择结帐页面等(这些是插件,这样就可以创建页面)。

无论哪种方式都表明您在构建主题的方法中存在根本缺陷(或者您正在为客户端构建某些东西,并且已决定使其易于安装,为您创建更多的工作,而您最好提供自己付费安装,这样会更有利可图)。

如果您足够愚蠢,无论如何都会尝试...

请参阅http://codex.wordpress.org/Function_Reference/wp_insert_post

但是,您将需要检查每个“ init”以查看页面是否存在。如果他们没有创建它们,并在某个地方更新选项,以使您记住它们已经创建,并且下次不进行检查,那么最终您将在每次加载页面时创建更多页面。


0

使用此答案中的代码以及wp_insert_post我在此主题中找到的代码。

wp_register_theme_activation_hook('twentyten', 'wpse_25885_theme_activate');
wp_register_theme_deactivation_hook('twentyten', 'wpse_25885_theme_deactivate');

/**
 *
 * @desc registers a theme activation hook
 * @param string $code : Code of the theme. This can be the base folder of your theme. Eg if your theme is in folder 'mytheme' then code will be 'mytheme'
 * @param callback $function : Function to call when theme gets activated.
 */
function wp_register_theme_activation_hook($code, $function) {
    $optionKey="theme_is_activated_" . $code;
    if(!get_option($optionKey)) {
        call_user_func($function);
        update_option($optionKey , 1);
    }
}

/**
 * @desc registers deactivation hook
 * @param string $code : Code of the theme. This must match the value you provided in wp_register_theme_activation_hook function as $code
 * @param callback $function : Function to call when theme gets deactivated.
 */
function wp_register_theme_deactivation_hook($code, $function)
{
    // store function in code specific global
    $GLOBALS["wp_register_theme_deactivation_hook_function" . $code]=$function;

    // create a runtime function which will delete the option set while activation of this theme and will call deactivation function provided in $function
    $fn=create_function('$theme', ' call_user_func($GLOBALS["wp_register_theme_deactivation_hook_function' . $code . '"]); delete_option("theme_is_activated_' . $code. '");');

    // add above created function to switch_theme action hook. This hook gets called when admin changes the theme.
    // Due to wordpress core implementation this hook can only be received by currently active theme (which is going to be deactivated as admin has chosen another one.
    // Your theme can perceive this hook as a deactivation hook.)
    add_action("switch_theme", $fn);
}

function wpse_25885_theme_activate()
{
    $default_pages = array(
        array(
            'title' => 'Home',
            'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consequat, orci ac laoreet cursus, dolor sem luctus lorem, eget consequat magna felis a magna. Aliquam scelerisque condimentum ante, eget facilisis tortor lobortis in. In interdum venenatis justo eget consequat. Morbi commodo rhoncus mi nec pharetra. Aliquam erat volutpat. Mauris non lorem eu dolor hendrerit dapibus. Mauris mollis nisl quis sapien posuere consectetur. Nullam in sapien at nisi ornare bibendum at ut lectus. Pellentesque ut magna mauris. Nam viverra suscipit ligula, sed accumsan enim placerat nec. Cras vitae metus vel dolor ultrices sagittis.'
            ),
        array(
            'title' => 'Contact',
            'content' => 'Duis venenatis augue sed risus laoreet congue ac ac leo. Donec fermentum accumsan libero sit amet iaculis. Duis tristique dictum enim, ac fringilla risus bibendum in. Nunc ornare, quam sit amet ultricies gravida, tortor mi malesuada urna, quis commodo dui nibh in lacus. Nunc vel tortor mi. Pellentesque vel urna a arcu adipiscing imperdiet vitae sit amet neque. Integer eu lectus et nunc dictum sagittis. Curabitur commodo vulputate fringilla. Sed eleifend, arcu convallis adipiscing congue, dui turpis commodo magna, et vehicula sapien turpis sit amet nisi.'
            )
    );
    $existing_pages = get_pages();
    $existing_titles = array();

    foreach ($existing_pages as $page) 
    {
        $existing_titles[] = $page->post_title;
    }

    foreach ($default_pages as $new_page) 
    {
        if( !in_array( $new_page['title'], $existing_titles ) )
        {
            // create post object
            $add_default_pages = array(
                'post_title' => $new_page['title'],
                'post_content' => $new_page['content'],
                'post_status' => 'publish',
                'post_type' => 'page'
              );

            // insert the post into the database
            $result = wp_insert_post($add_default_pages);   
        }
    }
}

function wpse_25885_theme_deactivate() 
{
   // code to execute on theme deactivation
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.