Shortcode IF statment help

时间:2017-07-24 作者:Anake.me

下面是我一直致力于运行短代码的代码。我已经尝试实施if() 声明,但没有任何效果。

我有两个输出。我想做的是,如果属性

(\'img\', \'user\', \'text\')
显示在快捷码中$output1.

否则,如果属性

(\'img\', \'text\', \'length\', \'material\', \'power\')
显示在快捷码中$output2.

function hire_equipment_func($atts) {

    extract( shortcode_atts( array(
        \'img\' => \'\',
        \'user\' => \'\',
        \'text\' => \'\',
        \'length\' => \'\',
        \'material\' => \'\',
        \'power\' => \'\',
    ), $atts ) );

    $atts = array_change_key_case((array)$atts, CASE_LOWER);

    return $output1;

    $output1 =

    \'<div class="hire-equipment-item">

        <div class="hire-equipment-item-img"><img src="\' . $atts[\'img\'] . \'" height=200px" width="200px" alt=""></div><br />

        <div class="hire-equipment-item-user">\' . $atts[\'user\'] . \'</div>

        <div class="hire-equipment-item-text">\' . $atts[\'text\'] . \'</div>

    </div>\';

    $output2 =

    \'<div class="hire-equipment-item">

        <div class="hire-equipment-item-img"><img src="\' . $atts[\'img\'] . \'" height=200px" width="200px" alt=""></div><br />

        <div class="hire-equipment-item-user">\' . $atts[\'user\'] . \'</div>

        <div class="hire-equipment-more-information">

            <table class="hire-equipment-more-information-table" cellpadding="15px">

                <tr>

                    <th>Length:</th>
                    <th>Material:</th>
                    <th>Power:</th>

                </tr>

                <tr>

                    <td> \' . $atts[\'length\'] . \' </td>
                    <td> \' . $atts[\'material\'] . \' </td>
                    <td> \' . $atts[\'power\'] . \' </td>

                </tr>

            </table>

        </div>

    </div>\';

}
add_shortcode( \'hire_equipment\', \'hire_equipment_func\' );
在此方面的任何帮助都将不胜感激!

1 个回复
最合适的回答,由SO网友:Cyclonecode 整理而成

我不太确定我是否理解这个问题。您希望根据在中设置的字段显示不同的标记$atts 大堆如果是这种情况,那么应该这样做:

// Check so fields are set in the `$atts` array.
if (isset($atts[\'img\'], $atts[\'user\'], $atts[\'text\'])) {
  return \'<div class="hire-equipment-item">
            <div class="hire-equipment-item-img">
              <img src="\' . esc_url($atts[\'img\']) . \'" height=200px" width="200px" alt="">
            </div><br />
            <div class="hire-equipment-item-user">\' . $atts[\'user\'] . \'</div>
            <div class="hire-equipment-item-text">\' . sanitize_text_field($atts[\'text\']) . \'</div>
         </div>\';
} 
elseif (isset($atts[\'img\'], $atts[\'text\'], $atts[\'length\'], $atts[\'material\'], $atts[\'power\'])) {
  // Second output goes here.
}
请注意user 在第二种情况下不能设置,否则第一个条件将始终为真。

由于两种条件的标记非常相似,因此更好的方法可能是:

function hire_equipment_func($atts) {
  // Store merged result. 
  $atts = shortcode_atts(array(
    \'img\' => \'\',
    \'user\' => \'\',
    \'text\' => \'\',
    \'length\' => \'\',
    \'material\' => \'\',
    \'power\' => \'\',
  ), array_change_key_case($atts, CASE_LOWER));


  $output = \'<div class="hire-equipment-item>\';

  // Check if image should be appended to output.
  if (isset($atts[\'img\'])) {
    $output .= \'<div class="hire-equipment-item-img">
                  <img src="\' . esc_url($attribs[\'img\']) . \'" height="200" width="200" alt="" />
                </div>\';
  }
  // Check if user should be appended to output.
  if (isset($atts[\'user\'])) {
    $output .= \'<div class="hire-equipment-item-user">\' . $atts[\'user\'] . \'</div>\';
  }
  // Check if text should be appended to output.
  if (isset($atts[\'text\'])) {
    $output .= \'<div class="hire-equipment-item-text">\' . $atts[\'text\'] . \'</div>\';
  }
  // Check if we need to output more-information table.
  if (isset($atts[\'length\']) || isset($atts[\'material\']) || isset($atts[\'power\'])) {
    $output .= \'<div class="hire-equipment-more-information">
                 <table class="hire-equipment-more-information-table" cellpadding="15px">
                   <tr>
                     <th>Length:</th>
                     <th>Material:</th>
                     <th>Power:</th>
                   </tr>
                   <tr>
                     <td> \' . (isset($atts[\'length\']) ? $atts[\'length\'] : \'\') . \' </td>
                     <td> \' . (isset($atts[\'material\']) ? $atts[\'material\'] : \'\') . \' </td>
                     <td> \' . (isset($atts[\'power\']) ? $atts[\'power\'] : \'\') . \' </td>
                   </tr>
                 </table>
               </div>\';
  }
  $output .= \'</div>\';

  return $output;
}
您可能还需要对输出进行一些消毒。例如使用esc_url() 对于URL和sanitize_text_field() 用于字符串等。

结束

相关推荐

shortcode get thumbnail size

如何获取短代码缩略图大小?我的代码在函数中获取短代码缩略图大小:function thumb_medium( $atts, $content = null ) { return wp_get_attachment_url( get_post_thumbnail_id( $post_id, \'medium\') ); //or wp_get_attachment_url( get_post_thumbnail_id( $post_id, \'large\') ); //or