problem including page template

I am creating a one page wordpress site. The code I am using does work but for some reason it will not include page templates. It only gets the content from the page editor but will not get the custom html that is inside the page template so I am trying to include the file.

I am getting error messages that say include() : “Failed opening”

Read More
 $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); 
 $file = get_template_directory_uri() . '/' . $template;
 include($file);

I am confused here because the path that it is giving me is the correct path for the file. I have 4 templates in my theme and from the code above, One template did get included but the other three have failed.

Related posts

Leave a Reply

1 comment

  1. I think you meant to use:

    get_template_directory()
    

    Codex: Absolute path to the directory of the current theme (without the
    trailing slash). (link)

    instead of:

    get_template_directory_uri()
    

    Codex: Retrieve template directory URI for the current theme. Checks for SSL. (link)

    if you’re using include() for PHP code.

    But you should rather consider using:

    get_page_template()
    

    Codex: Retrieve path of page template in current or parent template.
    (link)

    The function get_template_part() is also handy when want to resuse template parts.