Wordpress自定义post_type

时间:2013-01-15 14:34:20

标签: php wordpress-plugin wordpress

我想在我的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();
}

//编辑:已解决

1 个答案:

答案 0 :(得分:0)

has_archive设置为true会在您提到的位置显示帖子类型存档。

或者您可以将其留给false并使用slug movie_reviews创建一个页面

确保在进行更改以刷新重写规则后访问管理员中的永久链接设置页面。