0

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

// 编辑:已解决

4

1 回答 1

0

设置has_archivetrue将在您提到的位置显示帖子类型存档。

或者您可以保留它false并使用 slug 创建一个页面movie_reviews

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

于 2013-01-15T23:49:47.643 回答