Niharika Parida Niharika Parida - 7 months ago 10
PHP Question

How to create a shortcode for WordPress using Conditional Custom Post Get Field

How to create a shortcode for WordPress using a custom post code. Here I have some custom fields generated by ACF plugin and some conditions:

<?php
$price = get_field('price',$post->ID);
if(!empty($price)) {
echo get_post_meta($post->ID, 'price', true);
} else {
echo('consult price');
}
wp_reset_query();
?>


I want to create a shortcode using this HTML code:

function Amenities() {

return '<table class="datos-coche table table-bordered">
<tbody>
<tr>
<th scope="col"><img class="aligncenter size-full wp-image-5091" src="http://sitename.com/wp-content/uploads/2012/10/car_icon.png" alt="car_icon" width="30" height="25" /></th>
<th scope="col"><img class="aligncenter size-full wp-image-5095" src="http://sitename.com/wp-content/uploads/2012/10/door_icon.png" alt="door_icon" width="30" height="25" /></th>
<th scope="col"><img class="aligncenter size-full wp-image-5093" src="http://sitename.com/wp-content/uploads/2012/10/user_icon.png" alt="user_icon" width="30" height="25" /></th>
<th scope="col"><img class="aligncenter size-full wp-image-5094" src="http://sitename.com/wp-content/uploads/2012/10/fuel_icon.png" alt="fuel_icon" width="30" height="25" /></th>
<th scope="col"><img class="aligncenter size-full wp-image-5096" src="http://sitename.com/wp-content/uploads/2012/10/transmission_icon.png" alt="transmission_icon" width="30" height="25" /></th>
<th scope="col"><img class="aligncenter size-full wp-image-5092" src="http://sitename.com/wp-content/uploads/2012/10/engine_icon.png" alt="engine_icon" width="30" height="25" /></th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>';
}

add_shortcode('amenities', 'Amenities');


For each
td
tag, I want to insert above some PHP code using
get_field()
.

I have tried a lot methods, but the site got down.

Please help I am new to wordpress.

Thanks!

Answer

Try something like this or much better ...

    <?php

    function Amenities() {

        $price = get_field('price',$post->ID);
        $carbuilt = get_field('carbuilt',$post->ID);
        $door = get_field('door',$post->ID);
        $persons = get_field('persons',$post->ID);
        $fuel = get_field('fuel',$post->ID);
        $transmission = get_field('transmission',$post->ID);
        $engine = get_field('engine',$post->ID);

        if(!empty($carbuilt) && !empty($door) && !empty($persons) && !empty($fuel) && !empty($transmission) && !empty($engine)) {

        return "<table class='datos-coche table table-bordered'>
        <tbody>
        <tr>
          <th scope='col'><img class='aligncenter size-full wp-image-5091' src='http://sitename.com/wp-content/uploads/2012/10/car_icon.png' alt='car_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5095' src='http://sitename.com/wp-content/uploads/2012/10/door_icon.png' alt='door_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5093' src='http://sitename.com/wp-content/uploads/2012/10/user_icon.png' alt='user_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5094' src='http://sitename.com/wp-content/uploads/2012/10/fuel_icon.png' alt='fuel_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5096' src='http://sitename.com/wp-content/uploads/2012/10/transmission_icon.png' alt='transmission_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5092' src='http://sitename.com/wp-content/uploads/2012/10/engine_icon.png' alt='engine_icon' width='30' height='25' /></th>
        </tr>
        <tr>
          <td>".$carbuilt."</td>
          <td>".$door."</td>
          <td>".$persons."</td>
          <td>".$fuel."</td>
          <td>".$transmission."</td>
          <td>".$engine."</td>
        </tr>
      </tbody>
     </table>";
    } else {
     "<table class='datos-coche table table-bordered'>
        <tbody>
        <tr>
          <th scope='col'><img class='aligncenter size-full wp-image-5091' src='http://sitename.com/wp-content/uploads/2012/10/car_icon.png' alt='car_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5095' src='http://sitename.com/wp-content/uploads/2012/10/door_icon.png' alt='door_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5093' src='http://sitename.com/wp-content/uploads/2012/10/user_icon.png' alt='user_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5094' src='http://sitename.com/wp-content/uploads/2012/10/fuel_icon.png' alt='fuel_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5096' src='http://sitename.com/wp-content/uploads/2012/10/transmission_icon.png' alt='transmission_icon' width='30' height='25' /></th>
          <th scope='col'><img class='aligncenter size-full wp-image-5092' src='http://sitename.com/wp-content/uploads/2012/10/engine_icon.png' alt='engine_icon' width='30' height='25' /></th>
        </tr>
        <tr>
          <td> N.A </td>
          <td> N.A </td>
          <td> N.A </td>
          <td> N.A </td>
          <td> N.A </td>
          <td> N.A </td>
        </tr>
      </tbody>
     </table>"; 
    }
        wp_reset_query();
    }
    ?>