0

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?

2
  • ob_start() and ob_get_*() is generally the easy way to do it. All I can think is that search-select.php doesnt' exist in your theme (which is what get_template_part('search', 'select'); is looking for), and that you're setting $house_types outside the template. Variables don't get passed to get_template_part() like that. Commented Jan 19, 2018 at 0:20
  • The problem is, this logic should be inside the shortcode, not outside or inside the template. Is there any other solution for this, when the shortcode has a big HTML body?? Thanks! Commented Jan 19, 2018 at 0:24

2 Answers 2

1

Your core issue is that you pass value as globals but never declare them as such.

Generally speaking, "template parts" are useful when you want to give the ability to someone else to override them via a child theme or plugin, but if you are doing a "one off" theme, it is just better to write a function that generates the form and pass to it the relevant parameters instead of using global variables.

1
  • Thanks Mark! I think you are right and I could understand why. =) Commented Jan 22, 2018 at 23:08
0
<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>

<?php echo do_shortcode('[rci-search-houses]');?>
1
  • code dumps are not answers. Explain The why and how this code should solve the issue. Not even sure it actually answers what the OP wants. Commented Jan 19, 2018 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.