如何更改分类的自定义固定链接?(去掉Term-slug中的斜杠)

时间:2020-10-06 作者:Tereska

我有注册自定义分类法的代码。

function themename_custom_taxonomies() {
    // Coaching Method

    $coach_method = array(
        \'name\' => _x( \'Nieruchomość\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Nieruchomość\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Szukaj nieruchomości\' ),
        \'all_items\' => __( \'Wszystkie nieruchomości\' ),
        \'most_used_items\' => null,
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edytuj\' ), 
        \'update_item\' => __( \'Zapisz\' ),
        \'add_new_item\' => __( \'Dodaj nieruchomość\' ),
        \'new_item_name\' => __( \'Nowa nieruchomość\' ),
        \'menu_name\' => __( \'Nieruchomości\' ),
    );
    $args = array(
        \'hierarchical\' => true,
        \'labels\' => $coach_method,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'listings/?_prop=\', \'with_front\' => false)
    );
    register_taxonomy( \'nieruchomosc\', array( \'nieruchomosc\', \'ogloszenia\' ), $args );
}
此时,我的分类permalinks看起来:

https://example.com/listings/?_prop=/term-slug/
如何更改(没有斜杠):

https://example.com/listings/?_prop=term-slug
提前感谢您的帮助。

1 个回复
SO网友:Tereska

我找到了解决方案。。。

add_filter( \'term_link\', \'remove_slash_taxonomy\', 10, 2 );

function remove_slash_taxonomy( $permalink, $term ) {
        if ($term->taxonomy == \'nieruchomosc\') $permalink = str_replace(\'/\'.$term->slug.\'/\', $term->slug, $permalink);
        return $permalink;
}

相关推荐