通过传入信息数组以编程方式创建多个页面

时间:2014-01-17 作者:nobody_here

我正在努力适应Tom McFarlin\'s tutorial on programatically creating a page 进入一个函数,该函数将通过传入一个信息数组来创建多个页面。该页面在Wordpress中创建,但页面信息全部为空。

2 个回复
最合适的回答,由SO网友:nobody_here 整理而成

我自己想出来的。代码放在我的主题自定义函数文件中。

首先,我错误地传递了数组数据。其次,我调用函数的方式不正确。我相信有一种方法可以解决这个问题,但这是最后一段有效的代码。

$pages = array(
    $page_onair => array(
        \'page_id\' => -1,
        \'slug\' => \'radio-shows\',
        \'author_id\' => 1,
        \'title\' => \'On Air Shows\',
        \'tpl\' => \'template-onair.php\',
    ),
  $page_weather => array(
        \'page_id\' => -1,
        \'slug\' => \'weather\',
        \'author_id\' => 1,
        \'title\' => \'Area Weather\',
        \'tpl\' => \'template-weather.php\',
  ),
);
foreach($pages as $pinfo){
    //print_r($pinfo);
    $page_id = $pinfo[\'page_id\'];
    $slug = $pinfo[\'slug\'];
    $author_id = $pinfo[\'author\'];
    $title = $pinfo[\'title\'];
    $tpl = $pinfo[\'tpl\'];

    $pnum += 1;
    $programatically_create_pages = \'programatically_create_pages\' + $pnum;
    $programatically_create_pages = function($page_id, $slug, $author_id, $title, $tpl)             {
        $page_id = $page_id;
        $slug = $slug;
        $author_id = $author_id;
        $title = $title;
        $tpl = $tpl;
        //if page doesnt exist create it
        if(null == get_page_by_title($title)){
            //set post id so we know it was successfully created
            $page_id = wp_insert_post(array(
                \'comment_status\' => \'closed\',
                \'ping_status\' =>\'closed\',
                \'post_author\' => $author_id,
                \'post_name\' => $slug,
                \'post_title\' => $title,
                \'post_status\' => \'publish\',
                \'post_type\' => \'page\',
                )
            );
            update_post_meta($page_id, \'_wp_page_template\', $tpl);
        } else {
            // use -2 to indicate the page exists
            $post_id = -2;
        }
    };

    add_filter(\'after_setup_theme\', $programatically_create_pages($page_id, $slug,     $author_id, $title, $tpl));
}

SO网友:Jeremiah Prummer

您的页面id($page_id) 在您的数组中,都是-1。你有没有试过让它们与众不同?

结束

相关推荐

WP_LIST_PAGES更改父项的子项和锚点

我正在获取页面和子页面列表wp_list_pages().我想更改父级<a> 和<ul> 小孩我找到了解决办法here in this WPSE thread.但这只会改变<ul>. 如何将类/属性添加到(<a> 和<ul>) 同时