2

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

1 Answer 1

2

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 )
    )
);
7
  • 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
    Commented Aug 23, 2012 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
    Commented Aug 23, 2012 at 4:49
  • Make sure you visit the permalinks page? :(
    – Jake
    Commented Aug 23, 2012 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
    Commented Aug 23, 2012 at 17:42
  • Looks like that worked! Odd. I must have done something completely wrong yesterday. Thanks!
    – tr3online
    Commented Aug 23, 2012 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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