Change permalink for one category only
-
Hello
We want to change permalinks for all site. But we want to do it progressively.
How can i change permalink only for one category?
Now our permalinks are
domain.com/%category%/%postname//
and the new one it’s
domain.com/%post_id%/%category&/%postname%/
Thank you so much
-
You should be able to work something out with the Rewrite API. By specifying a particular category in the regexp, the rewrite will only apply to that category.
Hello bcworkz
Thanks for your help. Can you make a simple example to do it? I read doc, but it’s hard for me
Thanks
It occurs to me there’s two ways to interpret your question. Do you want to change the way WP generates such URLs, or are the URL’s changed and you need to change how WP responds to the request?
I assumed the latter in my prior response. I had in mind something like this:
add_rewrite_rule('^([^/]*)/cat_name/([^/]*)?','index.php?p=$matches[1]','top');
Be warned I’m terrible at regular expressions and this probably will not work but it’s at least a starting point. The idea is it is only applied when “cat_name” is in the URL. The post ID is passed to the query processor as an URL parameter, and it doesn’t really need anything else provided. The post name is available as $matches[2] if need be. I hope this helps in some way, sorry I can’t be more confident in my suggestion.
Thank you very much for the answer
What I need is the first of your options. i want to Change the way that wordpress generates the url , but only on a particular category. Items that are in the specified category, must contain the post_id in the url, and the rest as this:
domain.com/%post_id%/%category&/%postname%/
The reason for this is that I want to change the way in which WordPress generate url progressively and see the behavior of Google SERPS, and detect possible cuts in the search engine positions.
Every month or every two months change URL from one category more, and so everything would be under control
I hope you can help me with this
Thank you very much again
Oops! Sorry for my confusion. But I’m relieved my wonky add_rewrite_rule() will not be put to the test ??
The solution to altering links depends on how the link is generated. This is often through
the_permalink();
orecho get_the_permalink();
. You’ll need to look through your template files so identify any other methods.To alter links generated by the methods mentioned, hook the filter ‘post_link’. Your filter function will need a conditional that checks the link passed to it for the category in question. If found, use PHP string manipulation functions to inject the post ID into the link and return the result. Your filter function is also passed the post object
$post
, so the ID is readily available as$post->ID
.Uff!!
I think that it’s most dificult ?? I try to search on google how can i do this.
Thank you very much
Hello.
I Can do it. I show my code
add_filter('post_link', custom_permalink, 10, 3); function custom_permalink($permalink, $post, $leavename) { // Get post $post = get_post($post_id); if (!$post) return $permalink; $category = get_the_category($post_id); if ($category[0]->category_parent!=3063) return $permalink; $permalink = get_category_link( get_the_category( $post->ID ) ); $permalink = $post->ID.str_replace(get_site_url(), "", $permalink ).$post->post_name.'/'; return $permalink; }
Now this permalink don’t work. I try to do this with this code, but don’t works
add_action('generate_rewrite_rules', 'add_rewrite_rules'); function add_rewrite_rules( $wp_rewrite ) { $new_rules['([^/]+)/documental/([^/]+)/?$'] = 'index.php?p='.$wp_rewrite->preg_index(1); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } Can anyone help me on rewrite rules to make permalink works? Thanks
I believe we can get your permalink working. I say this because I just determined that if your permalink ends with the post slug, it does not matter what other url parameters precede it, the post will load. For example, the following URLs will all result in the same post being loaded:
domain.com/sample-post-slug/
domain.com/documental/sample-post-slug/
domain.com/123/documental/sample-post-slug/
domain.com/321/any-random-text/sample-post-slug/I think one reason your rewrite rule is not working is you have no generate_rewrite_rule() call to trigger the action you hooked. You can also toggle your permalink settings back and forth in your admin panel to do this.
Another is the regexp has no asterisk ‘*’ to match to permalink parameters, you are only matching slashes. But you should not need a rewrite rule at all, as it turns out, as long as the post slug (name) occurs on the end of the permalink. My initial suggestion for a rewrite rule was misguided.
None of that matters, it’s just FYI. I see some problems with your permalink code. $post_id is not defined, and you overwrite the post object passed with an assignment you do not need. You do not need get_post(), the post object is already in $post. And when you get_the category() the first time, pass $post->ID, not $post_id.
Finally, the way you build your permalink will not work. Check the href attribute of your HTML output to see what I mean. Once your code is generating the correct href, you will see no rewrite rule is required.
Thank you so much for your help!!
I make it and works fine. I don’t know if can be better
add_filter( 'post_link', 'custom_permalink', 10, 2 ); function custom_permalink( $permalink, $post ) { // Get the categories for the post $post = get_post($post_id); $category = get_the_category($post_id); if ( $category[0]->cat_ID==250) { $permalink = trailingslashit( home_url('/gestion-documental/' . $post->ID .'/' . $post->post_name . '/' ) ); } } function custom_rewrite_rules( $wp_rewrite ) { $new_rules['/gestion-documental/([0-9]+)/([^/]+)/?$'] = 'index.php?name=$matches[2]'; $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; return $wp_rewrite; } add_action('generate_rewrite_rules', 'custom_rewrite_rules');
It’s a good solution? Works fine, but i don’t know if it can be done better.
Thanks again
RegardsI have a problem
As bcworkz says, links
domain.com / category / subcategory / post_id / post_name
and
domain.com / category / subcategory / post_nameAnd therefore I have a problem, because google has indexed me two url and I will be penalized for duplicate content
How I can prevent not work?
Oooo! I know the answer to that one without having to think too hard ??
You need to add a canonical meta tag to your header template that contains the “official” permalink to the post. See https://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394
- The topic ‘Change permalink for one category only’ is closed to new replies.