【问题标题】:how to use nested if else stament in shortcode [duplicate]如何在短代码中使用嵌套的 if else 语句[重复]
【发布时间】:2019-08-26 11:24:59
【问题描述】:

我正在尝试制作嵌套 if else 短代码,但它不起作用。 请任何人尽快提供帮助。

function fn_services_text_box($attr){

$name           = $attr['service_name'];
$icon           = $attr['icon_path'];
$text           = $attr['content_text'];
$link           = $attr['url_path'];

$output         = if($icon == ''){
                    $output        .='<div class="service-list-title"><h3>'.$name.'</h3></div>
                    <div class="service-list-txt">'.$text.'</div>                                    
                    <div class="service-list-readmore"><a href="'.$link.'">Know More</a></div>';
                }
                else if($link == ''){
                    $output        .='<div class="polygon-shape">
                        <div class="icon">
                            <img class="img" src="'.$icon.'" />
                        </div>
                    </div>
                    <div class="service-list-title"><h3>'.$name.'</h3></div>
                    <div class="service-list-txt">'.$text.'</div>  '; 
                }
                else{
                    $output        .='<div class="polygon-shape">
                        <div class="icon">
                            <img class="img" src="'.$icon.'" />
                        </div>
                    </div>
                    <div class="service-list-title"><h3>'.$name.'</h3></div>
                    <div class="service-list-txt">'.$text.'</div>                                    
                    <div class="service-list-readmore"><a href="'.$link.'">Know More</a></div>';
                }

return $output;

}

我通过将它添加到短代码映射器来在可视化作曲家中使用这个短代码。

【问题讨论】:

  • $attr 包含什么?你的函数是否返回任何东西?

标签: php wordpress shortcode


【解决方案1】:

请替换此行

$output = if($icon == ''){

$output = "";
if($icon == ''){

【讨论】:

  • 它就像魔术一样非常感谢。你能解释一下你为什么这样做 $output == "";
  • 最欢迎@PoonamKatpara 让我解释一下,我们不能用 if 语句直接声明变量 $output。所以我们需要首先像$outout = "" 一样声明变量为空,然后如果其他声明我们可以像你已经做的那样附加数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多