0

カスタムポストコードを使用してWordPressのショートコードを作成する方法。ここで私は、ACFのプラグインといくつかの条件によって生成されたいくつかのカスタムフィールドを持つ:条件付きカスタム投稿を使用してWordPressのショートコードを作成する方法フィールドを取得

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

私は、このHTMLコードを使用してショートを作成したい:

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

tdタグについて、私はいくつかの上に挿入したいですPHPコードget_field()を使用しています。

私はたくさんの方法を試しましたが、サイトがダウンしました。

私はwordpressを初めて利用しています。

ありがとうございます!

+0

のようなものを試してみてください。私はエコーの下でそれを呼び出す方法を知らない。 –

+0

あなたの提案に応じてコードが正常に動作しています。それをエコーする方法を教えてください。 –

答えて

0

私のために正常に動作上記のPHPコード、その古い既存のコードこのまたはより良い...

<?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(); 
    } 
    ?> 
+0

add_shortcode( 'アメニティ'、 'アメニティ'); – Sashi

+0

うまく動作します –

関連する問題