【问题标题】:How to remove /tag/ from WordPress url ( without a plugin! )如何从 WordPress url 中删除 /tag/ (没有插件!)
【发布时间】:2021-04-08 14:55:16
【问题描述】:

我需要从我的新 WP 网站的网址中删除 /category/ 和 /tag/。 我已经找到了一个很好的工作 sn-p 来删除 /category/(我放在 functions.php 中)。

但我也没有找到删除标签的有效解决方案。

到目前为止,我尝试过的是这段代码(除了 YOAST 设置、永久链接更改等):

function twr_remove_tag( $string, $type )  {           
    if ( $type != 'single' && $type == 'category' && ( strpos( $string, 'tag' ) !== false ) ) {              
        $url_without_tag = str_replace( "/tag/", "/", $string );
        return trailingslashit( $url_without_tag );          
    }      
    return $string;  
}     
add_filter( 'user_trailingslashit', 'twr_remove_tag', 100, 2);

这或多或少与删除 /category/ 的代码相同,我认为这非常接近。也许只是 $type 参数是错误的?我必须在这里使用什么?

【问题讨论】:

标签: php wordpress tags wordpress-theming


【解决方案1】:

你可以试试下面的代码-

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 );

【讨论】:

  • 不幸的是,这段代码适用于 /category,但不适用于我这边的 /tag
  • 尝试将“类别”替换为“标签”。那么它将适用于标签。
猜你喜欢
  • 2013-07-21
  • 2015-10-11
  • 2016-09-02
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 2018-03-20
  • 2018-07-20
相关资源
最近更新 更多