0

This is a strange task but I really faced with it. On the home page there is loop for showing all custom post type. And I need to have oportunity when user click to the cpt link from the home page display one type of the single page. And if user go to the custom post type from inner page show other single custom post type template. I found old answer about this on the stackoverflow But it does not work properly. The code from stackoverflow:

On the home page I added to the link something like this:

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

And in the custom post type single page add this chunk of code:

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
 }

At first glance, this makes sense, but it doesn't work. Please help to solve this.

4
  • If it's the same CPT why not use the query argument to modify the same template and render as per the input variable. This would help you to maintain the template easily. And what doesn't work? The template doesn't loads or you get some error?
    – Kumar
    Jan 4, 2022 at 6:05
  • @Kumar There are not any error but the page does not display properly. I have single-gallery.php for example in this file I placed the previews code for checking the POST request. But instead of the page I need, it displays the default single-gallery.php without content. Jan 4, 2022 at 6:25
  • Just checked out the previous code again. Now a empty page is just displayed, there is no transition to the template. Jan 4, 2022 at 6:47
  • TBH, the information provided here is bit limited. What is the actual code you've? What is the CPT name? Do you have single-CPT.php in your theme root directory? And is the code above added to that single file or not? Also is the single-other.php and single-gallery.php in root directory as well or not?
    – Kumar
    Jan 4, 2022 at 10:47

2 Answers 2

1

I think you need to work on thetemplate_include hook. https://developer.wordpress.org/reference/hooks/template_include

At this point you can add a condition and returning the correct template path

add_filter( 'template_include', 'redirect_single' );
function redirect_single( $template ){
if($_POST['template'] == 'gallery') {
    $template = path/to/your/template;
 }elseif($_POST['template'] == 'other'){
    $template = path/to/your/template;
 }
return $template;
}
1
  • Thank you! I just a little modifired the $template variable using the locate_template(); And all works great! Jan 7, 2022 at 19:19
0

If you're going this way, you should use $_GET and not $_POST. Because it looks like you are not making a post request.

So:

if ( isset($_GET['template']) && $_GET['template'] == 'gallery' ) {

Other than that, you should open PHP with <?php instead of <php.

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.