Custom post type routing with hierarchy

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

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 )
    )
);

Leave a Comment