Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to make my links like this:

site.com/article-sample-post

as "article" is a fixed prefix and "sample-post" is the post slug..

but that prefix affect the category link structure making it :

site.com/article-category/sample-cat

which is not desired!

my question: does there any simple hack or plugin so i can make working permalinks like this:

site.com/article-sample-post
site.com/sample-cat

Thanks,

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If you want to remove the category base /category/, you can install the WP no category base plugin which will add a new set of rewrite rules for categories. the side-effect of this is that you can set your post permalinks to /article-%postname%/ and it will no longer effect category permalinks. it will still however effect tags.

Your other option, or in conjunction with the above plugin, is to filter post_link to add the article- prefix, then modify incoming requests to remove that prefix so WordPress can find the post:

add_filter( 'post_link', 'wpa68069_post_prefix' );
function wpa68069_post_prefix( $url ) {
    $prefix = 'article-';
    $parts = explode( '/', $url );
    return home_url( '/' ) . $prefix . $parts[ count( $parts ) - 2 ] . '/';
}


add_action( 'parse_request', 'wpa68069_parse_request' );
function wpa68069_parse_request( $request ){        
    if( !isset( $request->query_vars['name'] ) )
        return $request;

    if( false !== strpos( $request->query_vars['name'], 'article-' ) )
        $request->query_vars['name'] = substr( $request->query_vars['name'], 8 );

    return $request;
}
share|improve this answer
    
thx for help but, actually none of both options work... no category base removes the base but not the prefix! like this "site.com/article-sample-cat".... second option broken too! it echo the links for all posts like this "site.com/article-site.com" –  johnmido Oct 12 '12 at 18:03
    
both are tested and working with latest WordPress and TwentyEleven theme. What version are you using? What are your permalink settings? –  Milo Oct 12 '12 at 18:13
    
yes you're right "no category base" working but i was using "no category parent" instead, cause i want only one cat in link addition to no base... but the code snippet still not working and generates the above link for all posts. i'm using the latest wordpress with twentyeleven theme. posts structure "/article-%postname%.html" –  johnmido Oct 12 '12 at 18:23
    
ah, ok, the post_link function assumes your permalink settings are /%postname%/, you'll have to modify it to return the correct link: return home_url( '/' ) . $prefix . $parts[ count( $parts ) -1 ]; –  Milo Oct 12 '12 at 18:37
    
also- in case it' not clear, your permalink settings should not have article- in them, that's the whole point of the above code, it's added dynamically. –  Milo Oct 12 '12 at 18:38

This simple function add /articles before each post link eg. http://www.example.com/my-new-one-post/ will be change to http://www.example.com/articles/my-new-one-post/

I have permalink structure set to /%postname%/. Links like http://www.example.com/new-article

add_filter(
    'post_link', function ($url) {
        $parts = parse_url($url);

        if ($parts['path']) $parts['path'] = '/articles' . $parts['path'];
        $parts['scheme'] .= '://';
        if (isset($parts['query'])) $parts['query'] = '?' . $parts['query'];
        if (isset($parts['port'])) $parts['port'] = ':' . $parts['port'];
        if (isset($parts['fragment'])) $parts['fragment'] = '#' . $parts['fragment'];

        return implode('', $parts);
    }
);

The advantage of this solution is that preview links and short links (http://www.example.com/articles/?p=3677) also works well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.