1

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"

 $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.

0

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.

  • get_template_directory did the same thing for me as get_template_directory_uri. I tried get_page_template and that seems to work out ok. I think my code is messed up a little though because it puts the footer before the main content. But all the pages that I need are there and the content is correct. – Jamie Apr 8 '14 at 4:02
  • ok, but I wonder why you need to find the current page template for a one page site? So you are not using a static frontpage assigned to a custom template? – birgire Apr 8 '14 at 4:09
  • I am using a page template for the homepage. I am trying to include all templates and pages on the front page. Get_pages doesn't work because it won't get the HTML in the template. Are you thinking there is a better way? I am open to your suggestions – Jamie Apr 8 '14 at 11:57

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.