繁体   English   中英

分类页面永久链接更改wordpress

[英]Category page permalink change 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;
}

我找到了这个方便的解决方案,无需插件即可使用。 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 );

如果您想要一个用于删除类别的插件,请点击此处

将此粘贴到您的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM