显示自定义帖子类型列表

时间:2013-10-18 作者:codecowboy

以下函数可从插件启用单个自定义帖子类型模板:

function ch4_br_template_include( $template_path ) {
           if ( get_post_type() == \'book_reviews\' ) {
               if ( is_single() ) {
                   // checks if the file exists in the theme first,
                   // otherwise serve the file from the plugin
                   if ( $theme_file = locate_template( array
                       ( \'single-book_reviews.php\' ) ) ) {
                       $template_path = $theme_file;
                   } else {
                       $template_path = plugin_dir_path( __FILE__ ) .
                           \'/single-book_reviews.php\';
} }
}
           return $template_path;
       }
显示自定义帖子类型列表的等效方法是什么?我想有一个单独的模板,用于在mysite/resellers上显示“分销商”的表格。

以下是我的自定义帖子类型:

public function wps_reseller_cpt() {
        $labels = array(
            \'name\'               => _x( \'WPS Resellers\', \'post type general name\' ),
            \'singular_name\'      => _x( \'WPS Reseller\', \'post type singular name\' ),
            \'add_new\'            => _x( \'Add New\', \'book\' ),
            \'add_new_item\'       => __( \'Add New Reseller\' ),
            \'edit_item\'          => __( \'Edit Reseller\' ),
            \'new_item\'           => __( \'New Reseller\' ),
            \'all_items\'          => __( \'All Resellers\' ),
            \'view_item\'          => __( \'View Reseller\' ),
            \'search_items\'       => __( \'Search Resellers\' ),
            \'not_found\'          => __( \'No resellers found\' ),
            \'not_found_in_trash\' => __( \'No Reseller found in the Trash\' ),
            \'parent_item_colon\'  => \'\',
            \'menu_name\'          => \'Resellers\'
        );
        $args = array(
            \'labels\'        => $labels,
            \'description\'   => \'Holds our resellers and reseller specific data\',
            \'public\'        => true,
            \'menu_position\' => 50,
            \'supports\'      => array( \'title\', \'editor\',\'image\', \'thumbnail\', \'custom-fields\' ),
            \'rewrite\' => array(\'slug\' => \'resellers\'),
            \'has_archive\'   => true,
        );
        register_post_type( \'wps-reseller\', $args );
    }

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

而不是使用\'single_template\' 过滤器使用更通用的\'template_include\' 存档和单个后期视图的筛选器:

add_filter(\'template_include\', \'my_custom_templates\');

function my_custom_templates( $template ) {
  if ( is_single() && ( \'book_reviews\' == get_post_type() ) ) {
    if ( ! locate_template(\'single-book_reviews.php\', false) )
       $template = plugin_dir_path( __FILE__ ) . \'/single-book_reviews.php\';
  } elseif( is_post_type_archive(\'book_reviews\') ) {
     if ( ! locate_template(\'archive-book_reviews.php\', false) )
       $template = plugin_dir_path( __FILE__ ) . \'/archive-book_reviews.php\';
  }
  return $template;
}
之后,您只需创建文件\'archive-book_reviews.php\' 在插件文件夹中。

结束

相关推荐