【问题标题】:Wordpress remove parent category in URL (Domain mapping)Wordpress 删除 URL 中的父类别(域映射)
【发布时间】:2017-05-27 21:27:28
【问题描述】:

以下代码允许我删除我的主域帖子的父类别。 我的问题是我正在使用第三方插件来映射我的作者姓名中的第二个域。

  • domain1.com 主域
  • domain2.com 映射的域

固定链接结构:

  • domain1.com/me/cat/subcat/postname => domain2.com/cat/subcat/postname

所以基本上,domain1.com/me 映射到 domain2.com 并且它以这种方式工作得很好

但是如果我删除 cat slug 并只让 subcat 像这样(使用脚本):

  • domain1.com/me/subcat/postname 工作中
  • domain2.com/subcat/postname 不工作 (ERR_TOO_MANY_REDIRECTS)

删除 URL 中父 slug 的脚本

add_filter( 'post_link', 'remove_parent_category', 10, 3 );
function remove_parent_category( $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 ) {
            // Find parent categories and replace them in the link
            $parentcats = get_category_parents( $parent, false, '/', true );
            $permalink = str_replace( $parentcats, '', $permalink );
        }
    }
    return $permalink;
}

【问题讨论】:

    标签: wordpress categories slug


    【解决方案1】:

    看看像 WP remove category base 这样的插件,你可能会连接到 query_vars、category_link、request、category_rewrite_rules 来完成它。

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 2013-07-20
      • 1970-01-01
      • 2014-08-17
      • 2016-08-11
      • 2020-03-16
      • 2016-06-25
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多