类别页面固定链接更改wordpress

时间:2017-11-29 06:52:55

标签: php wordpress

目前我的永久链接: 子类别页面: 目前的网址:

https://<domain>/news/cloud/hybrid/
https://<domain>/news/cloud/private/

我想成为下面的事情:

https://<domain>/cloud/hybrid/
https://<domain>/cloud/private/

我尝试使用以下过滤器,但根据要求不能正常工作:

add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename )
{
    $cats = get_the_category( $post->ID );
    if ( $cats ) {
        usort( $cats, '_usort_terms_by_ID' ); 
        $category = $cats[0]->slug;
        if ( $parent = $cats[0]->parent ) {
            $parentcats = get_category_parents( $parent, false, '/', true );
            $permalink = str_replace( $parentcats, '', $permalink );
        }
    }
    return $permalink;
}

2 个答案:

答案 0 :(得分:0)

我找到了这个方便的解决方案,你可以在没有插件的情况下使用它。在SO

function fix_slash( $string, $type ) {
  global $wp_rewrite;
  if ( $wp_rewrite->use_trailing_slashes == false ) {
    if ( $type != 'single' && $type != 'category' )
      return trailingslashit( $string );

    if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
      return trailingslashit( $string );

    if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) ){
      $aa_g = str_replace( "/category/", "/", $string );
      return trailingslashit( $aa_g );
    }
    if ( $type == 'category' )
      return trailingslashit( $string );
  }
  return $string;
}

add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );

如果你想要一个插件来删除类别检查here

答案 1 :(得分:0)

将其粘贴到您的functions.php中,但也需要检查永久链接

// Remove category base
add_filter('category_link', 'no_category_parents',1000,2);
function no_category_parents($catlink, $category_id) {
    $category = &get_category( $category_id );
    if ( is_wp_error( $category ) )
        return $category;
    $category_nicename = $category->slug;

    $catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $category_nicename, 'category' );
    return $catlink;
}