0

I'm trying to display under a (very) custom URL a post in a template of my choice. My post's ID in this example is 1008 and my template name '/singles/lecture.php'.

Im the future will check the URL and add a condition when to do the action - now for tests the condition is just if (1) - which for the tests works for all URLs.

When I enter my custom URL, the template is retrieved and displayed without any problems, but there's a problem with the data.

In the function when I check the $post variable, there's some default $post object assigned - lets say its ID == 488, so I am trying to make the Wordpress to use the post which I need, so I make the get_post() and the setup_postdata() assignment the way codex says it should work, but it doesn't.

The $post data which is available within my /singles/lecture.php is back the data of the initial "default" post (i.e. 488).

add_filter('template_include', 'my_custom_urls');
function my_custom_urls($original_template) {
    global $post;

    if (1) {
        // second correct but not working alternative
        // $post = get_post(1008);
        // setup_postdata($post);

        $post_object  = get_post(1008);
        setup_postdata($GLOBALS['post'] =& $post_object);

        return get_template_directory() . '/singles/lecture.php';
    } else {
        return $original_template;
    }
}

What am I doing wrong? How to set the $post variable this way so it would be available to my template.

1

Should all CPT "lecture" posts use this template? If so, just rename your file single-lecture.php and place it in the root directory of your theme, and WP will automatically use it due to its template hierarchy.

If instead you're trying to assign this custom template to hand-picked pages, wherever you register your post type, add support for "page-attributes" and set your template up as a custom template by adding comments at the top.

CPT:

register_post_type('lecture'),
    array(...
        ...
        'supports' => array('title', 'editor', 'page-attributes'),
);

Template:

<?php
/*
 * Template Name: Customized Lecture
 */
...

You may need to unregister your CPT and then re-register it with this new code so WP recognizes the new attributes. Once your CPT has "page-attributes" you will see a new dropdown on the post editing screen, just like you see on Pages, that lets you select a template. You can then choose your template by whatever name you used in the comments at the top of the file.

  • No, it is totally crazy URL concept from my client, where to know which template should I use I have to analyze the whole given URL and it doesn't contain a unique slug as such, but rather set of URL parameters makes it unique. I am trying different approaches. Now I was hoping I can template_include the problem, but I cannot assign the $post object permanently as describe above :( – Picard Jun 1 '17 at 15:08
  • So if all the pages at that slug should have conditional templates, I'd suggest using a single template for that post type, like single-lecture.php, and within that template check the $_GET parameters and have if/else statements that display whatever you want depending on the parameters. – WebElaine Jun 1 '17 at 19:42
  • Unfortunately I have more CPTs, more templates and more crazy URLs so I am looking for a more centralized solution, but your suggestion could be a my resort - I will investigate the problem further. – Picard Jun 1 '17 at 19:54
  • A generic single.php that handles all the conditional logic might be your best bet. – WebElaine Jun 2 '17 at 15:45

Your Answer

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

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