I have created an events custom post type archive by custom date field and completed all related pages successfully.

It has :

  1. Archive page in the format : http://www.example.com/events/

  2. Archive by month (event date and not published date) list and having proper url structuring like: http://www.example.com/2015/11/events/

You can check this question which I asked recently to understand my approach towards creating the events which is archived by custom date field.

I created events archive page using get_template_part('content', get_post_type()); in archive.php page.

So the archive page name is content-events.php

Now I am not able to find a clean solution to make a gallery page for events which has:

  1. Images and name of the events listed by month.
  2. Archive list with some proper url structuring like : www.example.com/2015/11/gallery/events , www.example.com/2015/10/gallery/csrevents
  3. Suppose there are 2 types of events

    • events - /gallery/events
    • csrevents -/gallery/csrevents And a common url /gallery
  4. How can I route all three url's to same template file and then conditionally load the data based on url

I thought of following approach but I am not sure if it will be the correct way:

  1. To create a page programmatically using wp_insert_post and setting the page template to template-gallery.php and setting the slug as events-gallery

  2. Using WP_Query to get events post type by month in this page.

  3. Changing the url format to www.example.com/2015/11/gallery/events/in this plugin when the page name is template-gallery.php

  4. Using pre-get-post to highjack the url www.example.com/2015/11/gallery/events to get the month and year and setting post type as events. This shall return the posts for this month and year.

The main problem here is that I am trying to have 2 archive pages for same post type and this is making the process dirty.

I would like to know if there is a better way of doing this.

  • the cleanest way would probably be to add some extra rewrite rules. If you look at that plugin where the rules are generated, you can add some extras to handle /gallery/ at the end of each, and set your own query var so you can identify those requests when templates are loaded. – Milo Nov 7 '15 at 19:48
  • Thank @Milo for the quick reply. Am I correct on the templating part or I there is a way to do it through archives.php by adding some conditional tags. Is there a way I can make this page go through the archive page and use get_template_part so that I can reuse the archive page design for other custom post type gallery pages also. – roshan Nov 7 '15 at 19:55
  • You can force whatever template you want for any type of request by using the template filters and checking query vars or conditional tags in there. – Milo Nov 7 '15 at 20:01
  • @Milo I have been trying but not getting through. How do I open up a common template for /events/gallery , csrevents/gallery and allevents/gallery where allevents is not even a custom post type. And then conditionally load data based on the url. – roshan Nov 7 '15 at 21:03
  • I am stuck at step one itself i.e to open up a template on click of /events/gallery or csrevents/gallery . I tried using archive_template filter but I realized that events/gallery will never goes through it. Please help me. – roshan Nov 7 '15 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.