Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRichard Humulock
7,477 PointsURL Rewriting for custom post types...
So I'm trying to re-write the URL for a custom post type.
Currently, I have permalinks set to Post Name. So when I make my custom post types, the links come out as:
www.website.com/custom-post-type/name-of-the-post/
Now, for my site, I have 4 custom post types. I want them all to be under the same parent page. I also want them archived... so..
www.website.com/newsroom/custom-post-type/year/name-of-the-post/
is my desired URL structure.
I've read into the perma-link rewriting API and I guess I'm just confused as to how to go about this. I feel like I'm super close with 3 functions. I'm trying to use functions to add rules and do tag rewrites... so in my custom post type php file (the file that registers the post type) I can use rewrite -> array (rewrite rules) to get my desired outcome.
Somewhere along the lines I'm messing up. I was wondering if anyone could assist me in this.
3 Answers
Andrew Shook
31,709 PointsThis should work:
<?php
function custom_rewrite_basic() {
add_rewrite_rule('^newsroom/([^/]+)/([^/]+)/([^/]+)/?', 'index.php?post_type=$matches[1]& year=$matches[2]&pagename=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Richard Humulock
7,477 PointsI fixed it but this happened to be exactly what I needed. I didn't understand the format at first.
Thank you!!
Andrew Shook
31,709 PointsNo problem. I tried to get the make it as flexible as possible so that you could tweak it as needed.
Alfredo Mercedes Mejia
1,384 PointsI tried the code above and is not working for me, i am trying to do something similar but for some reason this is not working, help?, thanks
here is the scenario i have a custom post type named community_blog, links to post within it ends like this:
https://mysite.com/community_blog/my-sample-post
i want to be able to access my sample post via https://mysite.com/community_blog/my-sample-post and have the same post to display if the user types https://mysite.com/ca/community_blog/my-sample-post
here is the rewrite rule i am adding but is not working, is just redirecting to https://mysite.com/community_blog/my-sample-post when i try the ca version.
please help, i can get it to work, whats wrong, thank you so much :).
here is my version of the code above
function custom_rewrite_basic() { add_rewrite_rule('^ca/([^/]+)/([^/]+)/?', 'index.php?post_type=$matches[1]&pagename=$matches[2]', 'top');
} add_action('init', 'custom_rewrite_basic');