<?php get_template_part( 'template-parts/component-page/component-$variable); ?>

What is the correct way to add a variable to where I currently have $variable?


It looks like you're just trying to concatenate a string, so it can be done like this:

<?php get_template_part( 'template-parts/component-page/component-' . $variable ); ?>

Or you could use string interpolation (note the double quotes used here):

<?php get_template_part( "template-parts/component-page/component-{$variable}" ); ?>

Alternatively, get_template_part() can take two parameters, $slug and $name:

<?php get_template_part( 'template-parts/component-page/component', $variable ); ?>
| improve this answer | |

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.