我想在我的 wordpress 插件中使用自定义 post_type,但是如果我要访问这个 post_type,我会遇到问题。
如果我激活我的插件,我可以访问例如: http://wp.localhost/movie_reviews/movie_test/
类的一般部分
function __construct () {
add_action( 'init', array( &$this, 'my_int' ), 1 );
}
function my_int () {
add_filter( 'template_include', array(&$this, 'my_template' ), 10 );
register_post_type( 'movie_reviews',
array(
'labels' => array(
'name' => __('Movie Reviews', $this->var_sTextdomain),
'singular_name' => __('Movie Review', $this->var_sTextdomain),
'add_new' => __('Add New', $this->var_sTextdomain),
'add_new_item' => __('Add New Movie Review', $this->var_sTextdomain),
'edit' => __('Edit', $this->var_sTextdomain),
'edit_item' => __('Edit Movie Review', $this->var_sTextdomain),
'new_item' => __('New Movie Review', $this->var_sTextdomain),
'view' => __('View', $this->var_sTextdomain),
'view_item' => __('View Movie Review', $this->var_sTextdomain),
'search_items' => __('Search Movie Reviews', $this->var_sTextdomain),
'not_found' => __('No Movie Reviews found', $this->var_sTextdomain),
'not_found_in_trash' => __('No Movie Reviews found in Trash', $this->var_sTextdomain),
'parent' => __('Parent Movie Review', $this->var_sTextdomain)
),
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields', 'post-formats' ),
'taxonomies' => array( '' ),
'menu_icon' => plugins_url( 'img/my_icon.png', __FILE__ ),
'has_archive' => false,
'rewrite' => array('slug' => 'movie_reviews')
)
);
}
function my_template ( $template_path ) {
if ( get_post_type() == 'movie_reviews' ) {
if ( is_single() ) {
$template_path = plugin_dir_path( __FILE__ ) . '/templates/single-movie_reviews.php';
}
// etc ...
}
return $template_path;
}
在激活钩子上(课程的一部分)
function my_int () {
register_post_type( 'movie_reviews',
array(
'labels' => array(
'name' => __('Produkt Reviews', $this->var_sTextdomain),
'singular_name' => __('Produkt Review', $this->var_sTextdomain),
'add_new' => __('Add New', $this->var_sTextdomain),
'add_new_item' => __('Add New Produkt Review', $this->var_sTextdomain),
'edit' => __('Edit', $this->var_sTextdomain),
'edit_item' => __('Edit Produkt Review', $this->var_sTextdomain),
'new_item' => __('New Produkt Review', $this->var_sTextdomain),
'view' => __('View', $this->var_sTextdomain),
'view_item' => __('View Produkt Review', $this->var_sTextdomain),
'search_items' => __('Search Produkt Reviews', $this->var_sTextdomain),
'not_found' => __('No Produkt Reviews found', $this->var_sTextdomain),
'not_found_in_trash' => __('No Produkt Reviews found in Trash', $this->var_sTextdomain),
'parent' => __('Parent Produkt Review', $this->var_sTextdomain)
),
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields', 'post-formats' ),
'taxonomies' => array( '' ),
'menu_icon' => plugins_url( 'assets/img/plugin.png', __FILE__ ),
'has_archive' => true,
'rewrite' => array('slug' => 'movie_reviews')
)
);
flush_rewrite_rules();
}
// 编辑:已解决