【问题标题】:WP Permalinks: display just one kind of wp category in the posts urlWP Permalinks:在帖子 url 中只显示一种 wp 类别
【发布时间】:2017-01-18 03:43:56
【问题描述】:

我在我的网站/blog//news/ 中使用了2 个不同的类别。我想在我的帖子中显示 /news/ 类别,但我不想在博客帖子的 URL 中显示 /blog/ 类别。

我已经有很多帖子和每个类别的不同模板,所以我想避免创建页面来执行此操作。思路如下:

/news/ category --> index.php/news/post-name/

/blog/ category --> index.php/post-name/

这可能吗?

【问题讨论】:

    标签: wordpress url-rewriting categories permalinks posts


    【解决方案1】:

    需要做两件事。首先,使用 post_link 过滤器使您的帖子 URL 不同。

    function _20170117_post_link( $url, $post, $leavename ) {
        if( in_category( 'news', $post) ) { 
    
            $url = get_site_url() . '/news/' . $post->post_name ;
        }
        return $url;
    }
    add_filter( 'post_link', '_20170117_post_link', 10, 3 );
    

    其次,为此制定重写规则:

    function _20170117_rewrite() {
      add_rewrite_rule('^news/(.?.+?)(?:/([0-9]+))?/?$', 'index.php?pagename=$matches[1]
    &page=$matches[2]', 'top');
      add_rewrite_rule('^news/([^/]+)(?:/([0-9]+))?/?$', 'index.php?name=$matches[1]
    &page=$matches[2]', 'top');
      flush_rewrite_rules();
    }
    add_action('init', '_20170117_rewrite');
    

    注意flush_rewrite_rules();你不需要每次都执行。就一次。或者您可以转到 options-permalink.php 并单击保存。

    我使用以下设置进行了测试:

    settings

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-15
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多