Hi,
I'm using this code:
<?php include (TEMPLATEPATH . '/template.php'); ?>
... to pull in a template file, no problems here.
But now I would like to include a specific template file based on a custom field value.
This is the code I use to output the custom field value:
<?php echo $item['display_template_id']; ?>
So what I would like to achieve is to have the template id value attached to the file name.
Something like this:
<?php include (TEMPLATEPATH . '/templates/template-' . echo $item['display_template_id']'.php'); ?>
That way the user could for example make a selection of which template to be included.
I'm getting the following error with the above code:
syntax error, unexpected T_ECHO
Your help is much appreciated.
Thanks!
Martin Pham answers:
try this
<?php
include (TEMPLATEPATH . '/templates/template-' . $item['display_template_id'] . '.php');
?>
Edwin comments:
Thanks!
Navjot Singh answers:
Just remove the echo keyword. Use this
<?php include (TEMPLATEPATH . '/templates/template-' .$item['display_template_id']'.php'); ?>
Edwin comments:
Thanks!
Francisco Javier Carazo Gil answers:
You don't need to use echo.
Francisco Javier Carazo Gil comments:
The string will be well formed:
<?php include (TEMPLATEPATH . '/templates/template-' . $item['display_template_id']'.php'); ?>
Edwin comments:
That didn't work unfortunately. Thank you for your reply though.
Arnav Joy answers:
try this
<?php include (TEMPLATEPATH . '/templates/template-' .$item['display_template_id']'.php'); ?
paul de wouters answers:
there's WordPress native function for including templates which has the advantage of first looking in child theme first if there is one, which means you can override the parent theme.
get_template_part('/path_to/template.php');
or
get_template_part('loop','home'); // looks for loop-home.php
http://codex.wordpress.org/Function_Reference/get_template_part
Edwin comments:
Thanks Paul, I am aware of this function, but to my understanding this should only be used for true WordPress template parts, such as in your example a loop template.
http://justintadlock.com/archives/2010/11/17/how-to-load-files-within-wordpress-themes
In my case its just a php file with specific information.