wordpress后台默认有 文章、页面等类型,有时我们需要新增一些特殊类型,如作品等。
wordpress自定义文章类型
<?php//自定义文章类型:服务function post_type_service() {    $labels = array(        'name'               => __( '产品' ),        'singular_name'      => __( '产品' ),        'add_new'            => __( '新建产品' ),        'add_new_item'       => __( '新建产品' ),        'edit_item'          => __( '编辑产品' ),        'new_item'           => __( '新产品' ),        'all_items'          => __( '所有产品' ),        'view_item'          => __( '查看产品' ),        'search_items'       => __( '搜索产品' ),        'not_found'          => __( '没有找到有关产品服务' ),        'not_found_in_trash' => __( '回收站里面没有相关产品服务' ),        'parent_item_colon'  => '',        'menu_name'          => '产品服务'    );    $args = array(        'labels'        => $labels,        'description'   => '产品服务',        'public'        => true,        'menu_position' => 5,        'supports'      => array( 'title', 'editor', 'thumbnail' ),        'has_archive'   => true    );    register_post_type( 'service', $args );}add_action( 'init', 'post_type_service' );//自定义文章分类:服务function taxonomies_service() {    $labels = array(        'name'              => __( '产品分类'),        'singular_name'     => __( '产品分类' ),        'search_items'      => __( '搜索产品分类' ),        'all_items'         => __( '所有产品分类' ),        'parent_item'       => __( '该产品分类的上级分类' ),        'parent_item_colon' => __( '该产品分类的上级分类:' ),        'edit_item'         => __( '编辑产品分类' ),        'update_item'       => __( '更新产品分类' ),        'add_new_item'      => __( '添加新的产品分类' ),        'new_item_name'     => __( '新产品分类' ),        'menu_name'         => __( '产品分类' ),    );    $args = array(        'labels' => $labels,        'hierarchical' => true,    );    register_taxonomy( 'service_category', 'service', $args );}add_action( 'init', 'taxonomies_service', 0 );
二、自定义文章类型的固定链接修改
//自定义文章伪静态:服务
$post_types = array(
    'service' => 'service', //文章类型=>链接设置
);
add_filter('post_type_link', 'custom_post_link', 1, 3);
function custom_post_link( $link, $post = 0 ){
    global $post_types;
    if ( in_array( $post->post_type,array_keys($post_types) ) ){
        return home_url( $post_types[$post->post_type].'/' . $post->ID .'.html' );
    } else {
        return $link;
    }
}
add_action( 'init', 'custom_post_rewrites_init' );
function custom_post_rewrites_init(){
    global $post_types;
    foreach( $post_types as $k => $v ) {
        add_rewrite_rule(
            $v.'/([0-9]+)?.html$',
            'index.php?post_type='.$k.'&p=$matches[1]',
            'top' );
    }
}
获取自定义文章分类
<?php
    $args = array(
        'taxonomy' => 'service_category',    //自定义分类名称
        'hide_empty' => false,    //是否隐藏无文章的分类,默认true
        'count' => false,    //是否获取分类下文章数量,默认true
        'parent' => 0,    //父级ID,0是一级
    );
    $cats = get_terms($args);
    var_dump($cats);    //分类数组
?>
获取某个分类下的自定义文章
<?php
    $args = array(
        'numberposts' => '50',    //文章数量
        'post_type' => 'service',    //文章类型
        'tax_query'=>array(
            array(
                'taxonomy'=>'service_category',    //文章分类类型
                'terms'=>$cat_sub->term_id,    //分类ID,可以放多个分类的数组,或逗号分隔的字符串
            )
        ),
    );
    $posts = get_posts($args);
    if($posts): foreach($posts as $post):
?>
<a href="<?php the_permalink(); ?>" class="texts_a"><?php the_title();?></a>
<?php wp_reset_postdata(); endforeach; endif;?>
获取当前文章的类型
get_post_type();
获取当前文章的自定义分类
<?php 
$post_ID = get_the_ID(); 
$category = get_the_terms($post_ID,'service_category'); 
echo $category[0]->name;
?>
自定义分类列表页和自定义类型文章页
//自定义分类列表页
taxonomy-[taxonomy类型].php
//比如上面定义的service类型的列表页,注意名称 taxonomy-service_category.php
//自定义类型文章页
single-[文章类型].php
//比如上面定义的service类型文章 single-service.php
自定义分类列表页获取分类信息
//下面内容不需要修改
get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );