WordPress如何解决自定义文章类型指定文章模板失效的问题

导语:叶子将介绍WordPress如何生成自定义文章类型和在生成的过程中遇到的一些问题,包括如何使用原生态的分类与标签、如何使用自定义分类法、如何指定自定义文章类型的模板。

自定义文章类型

要创建一个自定义文章类型,需要使用register_post_type()函数。taxonomies参数可以使用WordPress原生态的分类目录、标签,它的值可以是array(‘category’,’post_tag’),这样就使用了WordPress自带的分类法和标签。

最后要将这个函数action到init中,下面是叶子创建的一个文章类型tour的详细代码。

function my_custom_post_tour() {
	
			$labels = array(
			'name'                => _x( 'Tours', 'Post Type General Name', 'whychinatours' ),
			'singular_name'       => _x( 'Tour', 'Post Type Singular Name', 'whychinatours' ),
			'menu_name'           => esc_html__( 'Tours', 'whychinatours' ),
			'all_items'           => esc_html__( 'All Tours', 'whychinatours' ),
			'view_item'           => esc_html__( 'View Tour', 'whychinatours' ),
			'add_new_item'        => esc_html__( 'Add New Tour', 'whychinatours' ),
			'add_new'             => esc_html__( 'New Tour', 'whychinatours' ),
			'edit_item'           => esc_html__( 'Edit Tour', 'whychinatours' ),
			'update_item'         => esc_html__( 'Update Tour', 'whychinatours' ),
			'search_items'        => esc_html__( 'Search Tours', 'whychinatours' ),
			'not_found'           => esc_html__( 'No Tours found', 'whychinatours' ),
			'not_found_in_trash'  => esc_html__( 'No Tours found in Trash', 'whychinatours' ),
		);
		$args = array(
			'label'               => esc_html__( 'tour', 'whychinatours' ),
			'description'         => esc_html__( 'Tour information pages', 'whychinatours' ),
			'labels'              => $labels,
			'supports'            => array( 'title', 'editor', 'thumbnail', 'author','excerpt', 'comments','custom-fields' ),
			'taxonomies'          => array('category','post_tag'),
			'hierarchical'        => false,
			'public'              => true,
			'show_ui'             => true,
			'show_in_menu'        => true,
			'show_in_nav_menus'   => true,
			'show_in_admin_bar'   => true,
			'can_export'          => true,
			'has_archive'         => true,
			'exclude_from_search' => false,
			'publicly_queryable'  => true,
			'capability_type'     => 'post',
			'rewrite' => array('slug' => 'tours'),
			'query_var' => true,
		);
		
		register_post_type( 'tour', $args );	
	}
add_action( 'init', 'my_custom_post_tour' );

自定义分类法

自定义文章类型除了可以通过taxonomies参数来使用WordPress的默认分类和标签,可以自定义分类法。下面是详细代码。

叶子定义了一个tours-category的分类法。


function my_taxonomies_tour() {
    $labels = array(
        'name'              => _x( 'Tour category', 'taxonomy 名称' , 'whychinatours'),
        'singular_name'     => _x( 'tour category', 'taxonomy 单数名称', 'whychinatours' ),
        'search_items'      => __( 'search tour' , 'whychinatours'),
        'all_items'         => __( 'Tour category' , 'whychinatours'),
        'parent_item'       => __( 'parent tour category' , 'whychinatours'),
        'parent_item_colon' => __( 'parent tour category' , 'whychinatours'),
        'edit_item'         => __( 'edit tour category' , 'whychinatours'),
        'update_item'       => __( 'update tour category' , 'whychinatours'),
        'add_new_item'      => __( 'add tour category' , 'whychinatours'),
        'new_item_name'     => __( 'add tour category' , 'whychinatours'),
        'menu_name'         => __( 'Tour category' , 'whychinatours'),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'tours-category', 'tour', $args );
}

add_action( 'init', 'my_taxonomies_tour', 0 );

自定义文章列表字段

当我们点击tours菜单上的All Tours,就会出现一个tour的列表,那么列表出现的字段,我们可以自己来定义,下面是定义的代码。

下面的代码输出定义的字段名。

add_filter('manage_tour_posts_columns', 'add_new_tour_columns');   
  
function add_new_tour_columns($tour_columns) {   
    //这个是前面那个选框,不要丢了   
    $new_columns['cb'] = '';   
  
    $new_columns['id'] = __('ID');   
    $new_columns['title'] =  __('Title');   

    $new_columns['categories'] = __('Categories');   
    
    $new_columns['tags'] = __('Tags');   
    $new_columns['tours-category'] = __('Tour Categories'); 
    
    $new_columns['date'] = _x('Date', 'column name');   
  
    //直接返回一个新的数组   
    return $new_columns;   
} 

下面的代码输出定义字段的内容。

add_action('manage_tour_posts_custom_column', 'manage_tour_columns', 10, 2);   
    
function manage_tour_columns($column_name, $id) {   
    global $wpdb;   
    switch ($column_name) {   
    case 'id':   
        echo $id;   
        break;   
    case 'tours-category':   
        //输出自定义分类字段
        
        the_terms( $post->ID, 'tours-category' ,  ' ' );
  
        break;  
    default:   
        break;   
    }   
}

如何指定自定义文章类型的模板

如果我们想要tour的文章使用另外的文章模板,那么我们可以创建一个single-tour.php的模板,那么所有tour的文章都会调用这个文章模板。

特别注意,如果后台没有将固定链接的设置改为自定义结构,那么这个模板是不会生效的。

结束

你学会了吗?

发表评论

邮箱地址不会被公开。 必填项已用*标注