重写自定义帖子类型(带分类法)URL

时间:2017-04-18 22:56:14

标签: wordpress url-rewriting

我有一个带有1个分类的自定义帖子类型,这是链接结构: mywebsite.com/casos-legal (在archive-casos-legal.php中显示帖子)

当我点击任何帖子时: mywebsite.com/casos-legal/lima/this-is-the-post-title

在上面的案例中," lima"是分类学的一个术语。

有没有办法在" casos-legal"之前添加路径?这样的事情: mywebsite.com/temas/legal/casos-legal

和: mywebsite.com/temas/legal/casos-legal/lima/this-is-the-post-title

更好的是,改变仅仅" casos"得到这样的东西: mywebsite.com/temas/legal/casos/lima/this-is-the-post-title

提前感谢!

这就是CTP的样子

function aprodeh_casos(){

$labels = array(
    'name' => 'Casos - legal',
    'singular_name' => 'Caso',
    'add_new' => 'Agregar',
    'all_items' => 'Todos',
    'add_new_item' => 'Agregar',
    'edit_item' => 'Editar',
    'new_item' => 'Nuevo',
    'view_item' => 'Ver',
    'search_item' => 'Buscar',
    'not_found' => 'No se encontraron casos',
    'not_found_in_trash' => 'No se encontraron casos en la papelera',
    'parent_item_colon' => 'Parent Item',

);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => '/casos-legal/%categoria_casos%', 'with_front' => false ),
    'has_archive' => 'casos-legal',
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array(
        'title',
        'editor',
        'thumbnail',
    ),
    'menu_position' => 5,
    'exclude_from_search' => false,
    'taxonomies' => array('post_tag'),
);
register_post_type('casos-legal',$args);

} add_action('init','aprodeh_casos');

这就是分类标准:

function aprodeh_casos_taxonomia(){

$labels = array(
    'name' => 'Categorías',
    'singular_name' => 'Categoría',
    'search_items' => 'Buscar categoría',
    'all_items' => 'Todas las categorías',
    'edit_item' => 'Editar categoría',
    'update_item' => 'Actualizar categoría',
    'add_new_item' => 'Agregar categoría',
    'new_item_name' => 'Nuevo categoría',
    'menu_name' => 'Categorías',

);

$args = array(

    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'casos-legal', 'with_front' => false ),

);

register_taxonomy('categoria_casos', array('casos-legal'), $args);

}

add_action('init', 'aprodeh_casos_taxonomia');

最后,这段代码为了得到这个链接结构:   mywebsite.com/casos-legal/lima/this-is-the-post-title

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

1 个答案:

答案 0 :(得分:0)

尝试更新

'rewrite' => array( 'slug' => '/casos-legal/%categoria_casos%', 'with_front' => false ),

'rewrite' => array( 'slug' => '/tema/legal/casos/%categoria_casos%', 'with_front' => false ),