I have a shortcode which will have a big HTML.
It will have 4 selects which I'm getting the data inside the shortcode:
$house_types = get_terms(array(
'taxonomy' => 'house_types',
'hide_empty' => 0
));
I would like to insert a template that can read this variable in order to build the select, my template file:
<label for="tipo">Tipos:</label>
<select name="tipo" id="house_types">
<option disabled selected value></option>
<?php
if(count($house_types) > 0) {
foreach($house_types as $house_type) {
echo '<option value="'.$house_type->term_id.'">'.$house_type->name.'</option>';
}
}
?>
</select>
I tried to use this, but it doesn't make sense to put the template inside the theme and not inside the plugin dir:
function rci_search_houses( $atts ) {
ob_start();
$house_types = get_terms(array(
'taxonomy' => 'house_types',
'hide_empty' => 0
));
get_template_part('search', 'select');
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('rci-search-houses', 'rci_search_houses');
But it doesn't show anything.
Anyone knows a better way to organize the shortcode in case it has a big html?
ob_start()
andob_get_*()
is generally the easy way to do it. All I can think is thatsearch-select.php
doesnt' exist in your theme (which is whatget_template_part('search', 'select');
is looking for), and that you're setting$house_types
outside the template. Variables don't get passed toget_template_part()
like that.