-1

I am developing a site for a company and I require the ability to have multiple template files for one custom post type. I have three landing pages that all display the same list of services/custom post type posts in a different manner; however, depending on the page the user is on, I want the inner template to change.

For instance, if the user is on the Gallery landing page and they click on the custom post type, I want the single-gallery template to be loaded. If the user is on the Testimonial landing page I want the single-testimonial template to be loaded and so on...

I have searched through WordPress and have found things like template_part and endpoints, but as a new user of WordPress I am in need of a bit more help!

Any help would be much appreciated. Thanks!

2 Answers 2

0

For a quick solution I would suggest something like this:

When linking you could use:

<a href="<php the_permalink();?>?template=gallery"> Gallery Page </a>

Then on the single-customposttype.php

if($_POST['template'] == 'gallery') {
 get_template_part('single', 'gallery'); // the file for this one is single-gallery.php
}elseif($_POST['template'] == 'other'){
 get_template_part('single', 'other'); // the file for this one is single-other.php
}

not the prettiest, but it's quick and easy to roll out.

1
  • Thanks. I have used that and it work great. Out of interest, what would be a better, prettier way of doing it?
    – Joshua H
    Oct 24, 2015 at 18:33
0
  1. Find out at which landing page we are (gallery, testimonials, other, ...)
  2. Query your posts using get_template_part()

For example:

if($thispage == 'gallery') {
 get_template_part('single', 'gallery'); // the file for this one is single-gallery.php
}elseif($thispage == 'other'){
 get_template_part('single', 'other'); // the file for this one is single-other.php
}

Or like that

get_template_part('single', $thispage ); 

https://codex.wordpress.org/Function_Reference/get_template_part

6
  • So I would just use the_permalink(); still, which would direct to a page with the code above, which would then choose the appropriate inner page? Thank you by the way. Been scratching my head for hours to no avail!
    – Joshua H
    Oct 23, 2015 at 12:32
  • the_permalink() returns the link to a post/page. I don't know how you define your landing pages (is it multisite or something else), according at which landing page user are you get him a template file. You need to retrieve landing page name and then load template for single post Oct 23, 2015 at 12:39
  • What I don't understand is how I can get the page that they are on? How do I put that into the variable $thispage?
    – Joshua H
    Oct 23, 2015 at 12:41
  • You need to retrieve landing page name and then load template for single post, I don't know how you define your landing pages Oct 23, 2015 at 12:42
  • I have three landing pages, the gallery is page-gallerylanding.php which pulls in the titles from the services custom post type. I have a page-serviceslanding which does the same but the page looks different. I also have a page-testimoniallanding which pulls in the same as gallerylanding but links to a different place
    – Joshua H
    Oct 23, 2015 at 12:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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