Getting WordPress to automatically show category permalink for only specific categories

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.

Answers


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.


Need Your Help

Using PHP's XMLReader, how do I get the line number of the current node?

php xml parsing xmlreader line-numbers

Using the XMLReader XML parser in PHP 5.3, I need to get the line number of the current node. A column number or total offset from the beginning of the file would be nice, too.

Good profiler for Fortran and MPI

profiling fortran mpi profiler

Which profiler do you use for Fortran code base with MPI in it? gprof doesn't seem to be working correctly. Sun Studio Analyzer only returns the timings for the C/C++ system calls and none of the f...