Getting WordPress to automatically show category permalink for only specific categories

随声附和 提交于 2019-12-11 20:04:58

问题


I have searched, here is the closes result.

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.

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.


回答1:


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:

I want most posts to have no category in the URL

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

EDIT #1

To answer this:

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. –

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.

Try this

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

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

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.



来源:https://stackoverflow.com/questions/11024040/getting-wordpress-to-automatically-show-category-permalink-for-only-specific-cat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!