• Resolved charlesolaes

    (@charlesolaes)


    I found this code in the codex where it loads a custom template depending on the post type. Link

    <?php
    function get_custom_post_type_template($single_template) {
         global $post;
    
         if ($post->post_type == 'my_post_type') {
              $single_template = dirname( __FILE__ ) . '/post-type-template.php';
         }
         return $single_template;
    }
    
    add_filter( "single_template", "get_custom_post_type_template" ) ;
    ?>

    my question is, in the get_custom_post_type_template() function, what does the parameter $single_template do? I can see that it returns the absolute path of the template file, but why put it as a parameter when you’re going to reassign the value again, and if the $single_template isn’t assigned to the PATH then what does it return? a blank string?

Viewing 5 replies - 1 through 5 (of 5 total)
  • $single_template is the name of the template file to use if the post_type is NOT ‘my_post_type’.

    My guess is that is set to ‘single.php’ before the function is called.

    Thread Starter charlesolaes

    (@charlesolaes)

    so the value provided in the $single_template parameter is set by wordpress?

    I have not looked inside the code to verify that, but I think that is what is happening. Filters are generally a way for you to change what WordPress would normally use, so that is probably correct.

    Thread Starter charlesolaes

    (@charlesolaes)

    cool, thanks for clearing that up for me

    Alwyn Botha

    (@123milliseconds)

    Please mark thread as resolved so that

    – others with similar problem can see it as resolved and will read this thread for help if they have similar problem

    – people providing help see it as resolved and will not waste time reading this post.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need help understanding custom templates (question)’ is closed to new replies.