使用分类标记,以自定义帖子类型显示自定义网址

时间:2019-04-08 22:19:35

标签: php wordpress

我需要一个类似 www.example.com/taxonomy/taxonomy-slug/post-slug

我可以救你,但是出了点问题。 我在分类法中创建了两个项目; ITEM1和ITEM2 我创建了一个帖子,并坚持了这两个项目 在taxonomy.php页面上,我打开了两个页面,即

www.example.com/taxonomy/item1/
www.example.com/taxonomy/item2 /

到目前为止一切顺利,但是两个页面的帖子链接都是这样的 www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item1/post-slug

实际上,它应该作为
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item2/post-slug

我的下面的代码

//创建帖子类型

function custom_post_type1() {

    $labels = array(
        'name'                => _x( 'Curso', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Curso', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Curso', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Curso Relacionado', 'twentythirteen' ),
        'all_items'           => __( 'Todos os Curso', 'twentythirteen' ),
        'view_item'           => __( 'Ver Curso', 'twentythirteen' ),
        'add_new_item'        => __( 'Adicionar novo Curso', 'twentythirteen' ),
        'add_new'             => __( 'Adicionar novo', 'twentythirteen' ),
        'edit_item'           => __( 'Editar Curso', 'twentythirteen' ),
        'update_item'         => __( 'Atualizar Curso', 'twentythirteen' ),
        'search_items'        => __( 'Buscar por Curso', 'twentythirteen' ),
        'not_found'           => __( 'Nenhum Curso encontrado', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Nenhum Curso encontrado na lixeira', 'twentythirteen' ),
    );

//为“自定义帖子类型”设置其他选项

    $args = array(
        'label'               => __( 'curso', 'twentythirteen' ),
        'description'         => __( 'curso news and reviews', 'twentythirteen' ),
        'rewrite' => true,
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        'hierarchical'        => false,
        'menu_icon' => '',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite' => array( 'slug' => 'curso/%actor%', 'with_front' => false ),
        'has_archive' => 'curso'
    );

    // Registering your Custom Post Type
    register_post_type( 'curso', $args );

}

//创建分类法

function add_actor_taxonomy() {
  if (!is_taxonomy('actor')) {
    register_taxonomy('actor', 'curso' ,
      array(
        'hierarchical' => FALSE,
        'label' => __('actor'),
        'public' => TRUE,
        'show_ui' => TRUE,
        'query_var' => 'actor',
        'rewrite' => array( 'slug' => 'unidade', 'with_front' => false )
      )
    );
  }
}

add_action('init', 'add_actor_taxonomy');

//自定义永久链接

function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'curso' ){
        $terms = wp_get_object_terms( $post->ID, 'actor' );
        if( $terms ){
            return str_replace( '%actor%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

我希望返回以下内容
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item2/post-slug

您能看到我的代码有问题吗?

0 个答案:

没有答案