让 WordPress 自动显示特定类别的类别永久链接 [英] Getting WordPress to automatically show category permalink for only specific categories

查看:54
本文介绍了让 WordPress 自动显示特定类别的类别永久链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过,这里是关闭结果.

我正在建立一个新的 wordpress 网站.我希望大多数帖子在 URL 中没有类别,只需 www.site.com/title.但是我确实希望博客文章是分开的,所以我想要 www.site.com/blog/title.我还希望将来可以选择添加更多类似的内容,仅针对特定类别,而不是整个网站.

I am building a new wordpress site. I want most posts to have no category in the URL, simply www.site.com/title. However I do want the blog posts to be separate, so I'd like www.site.com/blog/title. I'd also like the option to add more like that in the future, for only specific categories, not the entire site.

stackoverflow 上有很多类似的问题,但大多数都有 0 个回复.任何建议都会很棒.我什至尝试过 Advanced Permalinks 没有任何运气.

There are many questions similar to this here on stackoverflow but most have 0 replies. Any advice would be great. I've even tried Advanced Permalinks without any luck.

推荐答案

您可以简单地通过设置 > 永久链接并添加到通用设置 > 自定义结构值 /blog/%postname%/.在那里,您可以从 www.site.com/blog/title 访问该博客文章.

You can simply do that by Setting > Permalinks and add to Common Setting > Custom Structure the value /blog/%postname%/. There you will get the blog post accessible from www.site.com/blog/title.

我无法理解第一个问题.作者:

I cannot understand the first question. By:

我希望大多数帖子在 URL 中没有类别

I want most posts to have no category in the URL

你的意思是没有 www.site.com/category/category-name 吗?或者没有 www.site.com/category/post?

do you mean to NOT HAVING www.site.com/category/category-name? or not having www.site.com/category/post?

编辑 #1

回答这个问题:

www.site.com/category/post 是我只想要类别为博客"的博客文章 > 如果类别是鞋子",我不希望在 URL 中显示类别.——

www.site.com/category/post is what I ONLY want for blog posts with the category of "Blog" > if the category is "Shoes" I don't want the category displayed in the URL. –

首先:您可以将固定链接设置为 /%postname%/ 以便您的所有帖子都具有站点/标题,因此可以从该链接访问

First: you can set the Permalink to /%postname%/ so all your post will have site/title therefore accessible from that link

第二:您必须过滤永久链接,以针对博客"类别下的帖子表现出不同的行为.

Second: You have to filter the permalink to behave differently for the posts under "Blog" category.

试试这个

add_filter( 'post_link', 'custom_permalink', 10, 2 );
function custom_permalink( $permalink, $post ) {
    // Get the categories for the post
    $categories = wp_get_post_categories( $post->ID );
    foreach ( $categories as $cat ) {
        $post_cat    = get_category( $cat );
        $post_cats[] = $post_cat->slug;
    }

    // Check if the post have 'blog' category
    // Assuming that your 'Blog' category slug is 'blog'
    // Change 'blog' to match yours
    if ( in_array( 'blog',$post_cats ) ) {
        $permalink = trailingslashit( home_url( 'blog/' . $post->post_name ) );
    }

    return $permalink;
}

第三:你必须过滤 rewrite_rules

Third: You have to filter rewrite_rules

add_filter( 'rewrite_rules_array', 'custom_rewrite_rule' );
function custom_rewrite_rule( $rules ) {
    $new_rules = array(
        'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
        'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
        'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]'
    );

    $rules = $new_rules + $rules;

    return $rules;
}

转到永久链接设置并保存设置以刷新重写规则并使上述更改生效

Go to permalink setting and save the setting to refresh your rewrite rules and make the changes above active

注意:将这些函数添加到您的活动主题 functions.php 模板

NOTE: Add those functions on your active theme functions.php template

注意:我还没有测试过,但这是您更改永久链接的方式.我以类似的方式更改了档案和搜索结果上的永久链接.

NOTICE: I haven't test it yet but that's the way you change permalink. I did the similar way to change my permalink on archives and search result.

这篇关于让 WordPress 自动显示特定类别的类别永久链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆