0

我创建了一个插件并使用此代码创建了一个菜单

add_filter('page_template', 'in_page_template');
function in_page_template()
{
    global $wpdb;
    $new_page_title = 'Packages';
    $sql = "SELECT * FROM wp_posts where post_name='" . $new_page_title . "';";
    $cnt_post = $wpdb->get_results($sql);
    if (!(is_page('Home'))) {
        $ppid = $_GET['page_id'];
        if (count($cnt_post) != 0) {
            $pid = $cnt_post[0]->ID;
            if ($pid == $ppid) {
                $page_template = dirname(__FILE__) . '/Packages.php';
                return $page_template;
            }
        }
    }
}

如何为包页面创建子菜单

4

1 回答 1

0
add_action('init', 'create_initial_pages');
function create_initial_pages() {
$pages = array(
    array(
        'name'  => 'post_name',
        'title' => 'post_title',
        'child' => array(
            'page1-1' => 'National',
            'page1-2' => 'International'

        )
    ),

 );

 $template = array(
    'post_type'   => 'page',
    'post_status' => 'publish',
    'post_author' => 1
 );

 foreach( $pages as $page ) {
    $exists = get_page_by_title( $page['title'] );
    $my_page = array(
        'post_name'  => $page['name'],
        'post_title' => $page['title']
    );
    $my_page = array_merge( $my_page, $template );

    $id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );

    if( isset( $page['child'] ) ) {
        foreach( $page['child'] as $key => $value ) {
            $child_id = get_page_by_title( $value );
            $child_page = array(
                'post_name'   => $key,
                'post_title'  => $value,
                'post_parent' => $id
            );
            $child_page = array_merge( $child_page, $template );
            if( !isset( $child_id ) ) wp_insert_post( $child_page );
         }
     }
   }
}
于 2012-09-21T06:30:34.877 回答