<?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?

1 Answer
1

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 ); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *