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've been playing around with a lot of custom post types lately and I am pretty anal about my URL structure / routing.

An issue I ran in to a with a site that I just developed was this:

  • I had many custom post types, for example: videos
  • The URL structure to 'view' the video list (ie: archive of sorts) was /media/videos/
  • This 'view' was basically a custom WP_Query displaying things properly from a custom .php template.
  • When an individual 'video' link is clicked you are taken to the post type URL structure, ie: www.site.com/videos/{slug}
  • This causes all loss of URL structure/parental relationship to structure.

I would love to solve this problem:

  • When you navigate to /media/videos/ you get the custom loaded template file
  • When you click a video permalink(), you will be linked to /media/videos/{slug}
  • Ideally this would perserve hierarchy of URL structure (so if something like Breadcrumb NavXT it would still have a relationship).

I made a teacher/student/etc module one time where I did custom routing via add_rewrite_rule to handle these problems, but I would rather figure out how to handle hierarchy appropriately in this situation.

Can someone please point me in to the right direction?

Thanks!

Tre

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

When you declare the custom post type, there is a parameter "rewrite" where you declare the slug for the post type. Change the slug to "media/videos", and then visit your Settings > Permalinks page to update your rewrite rules.

register_post_type( 'videos',
    array('labels' => array(),
        'rewrite'   => array( 'slug' => 'media/videos', 'with_front' => false )
    )
);
share|improve this answer
    
If I have a page called media/videos this will cause a permalink collision, correct? That's ideally what I'd want. media/videos => page & media/videos/{slug} => actual post type. I tried using 'slug' & 'with_front' => false, and I got collision –  tr3online Aug 23 '12 at 2:17
    
I don't have that problem. I defined the slug for the post type as 'media/videos' and then I created a top-level page with slug 'media' and child page with slug 'videos'. When I access 'media/videos', I get the page. When I access a video at 'media/videos/video-slug', I get the video with slug 'video-slug'. –  Jake Aug 23 '12 at 4:49
    
Make sure you visit the permalinks page? :( –  Jake Aug 23 '12 at 4:58
    
Hm. I'll have to take a look at this again. Did you create the post type before the pages? Though that shouldn't matter. –  tr3online Aug 23 '12 at 17:42
    
Looks like that worked! Odd. I must have done something completely wrong yesterday. Thanks! –  tr3online Aug 23 '12 at 18:45
show 2 more comments

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.