Steve,我的理解是你想要:
没有“所有帖子”子菜单的自定义帖子类型您只想在“帖子”子菜单下显示“编辑主页”链接吗您不希望用户为特定的帖子类型添加超过1篇的帖子下面的代码将调整WordPress管理员来执行上述操作:
注意:用户仍然可以使用将帖子添加到自定义帖子类型wp_insert_post
<?php
/**
* Plugin Name: Home Page CPT
**/
class WPSE_26330_Homepage_CPT {
function __construct() {
// add the default homepage on plugin activation
register_activation_hook( __FILE__, array( &$this, \'add_home_page_post\' ) );
// register the homepage post type
add_action( \'init\', array( &$this, \'register_homepage_cpt\' ) );
// add the menu link
add_action( \'admin_menu\', array( &$this, \'edit_homepage_link\' ) );
}
function edit_homepage_link() {
global $submenu, $pagenow;
// query the homepage posts
$homepage = new WP_Query( \'post_type=homepage\' );
// if its new post page and we have homepage
if ( $pagenow == \'post-new.php\' && $homepage->have_posts() ) {
wp_die(\'You cant add more then one homepage\');
}
// if we have homepage post, show the edit link else the add homepage link
if ( $homepage->have_posts() ) {
$homepage->the_post();
$link = get_edit_post_link( get_the_ID(), \'return\' );
$title = \'Edit Home Page\';
} else {
// in case if the user has deleted the default post
$link = get_bloginfo( \'url\' ). \'/wp-admin/post-new.php?post_type=homepage\';
$title = \'Add Home Page\';
}
$submenu[\'edit.php\'] = array( array( $title, \'manage_options\', $link ) ) + $submenu[\'edit.php\'];
}
function register_homepage_cpt() {
$args = array(
\'label\' => \'homepage\',
\'description\' => \'Home Page post type\',
\'public\' => true,
\'show_in_menu\' => false
);
register_post_type( \'homepage\', $args );
}
function add_home_page_post() {
// on activation first regsiter the post type
$this->register_homepage_cpt();
// add the first and only post
$post_data = array(
\'post_title\' => \'Home Page\',
\'post_type\' => \'homepage\',
\'post_statue\' => \'publish\',
\'post_author\' => 1
);
wp_insert_post( $post_data );
}
}
$GLOBALS[\'wpse_homepage_cpt\'] = new WPSE_26330_Homepage_CPT;
?>