普通网友 2015-03-03 20:03
浏览 49
已采纳

如何在wordpress短代码中使用php if语句?

I will try to make a WordPress plugin. My all code is ok but I can't understand how I use if else statement in custom post shortcode.

global $post;
        $accordion_icon = get_post_meta($idd, 'accordion_icon', true);

        $list .= '
                <div class="collapse-card">
                <div class="title">
                    <i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>
                    <strong>'.get_the_title().'</strong>
                </div>
                <div class="body">
                    <p>'.get_the_content().'</p>
                </div>
                </div>
                ';

It's my present code. Hear have a icon option in title div .I will want to when I put any custom icon code then show the custom icon and when I don't put any custom icon code then show a common icon. I can understand here need to use if else statement but I don't understand how I use if else statement in my code.

$list .= '
                <div class="collapse-card">
                <div class="title">
                '<?php if($accordion_icon) : ?>'
                    <i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>
                '<?php else : ?>'
                    <i class="fa fa-question-circle fa-2x fa-fw"></i>
                '<?php endif; ?>'

                    <strong>'.get_the_title().'</strong>
                </div>
                <div class="body">
                    <p>'.get_the_content().'</p>
                </div>
                </div>
                ';  

I used like that but it's not working.Show error .

So now how I can use if else statement ?

  • 写回答

1条回答 默认 最新

  • donjd86266 2015-03-03 20:16
    关注

    You can do it either like this (short way):

    $list .= '<div class="collapse-card">
              <div class="title">';
    if($accordion_icon)
        $list .= '<i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>">';
    else
        $list .= '<i class="fa fa-question-circle fa-2x fa-fw"></i>';
    

    Or like this (long way):

    $list .= '<div class="collapse-card">
              <div class="title">';
    if($accordion_icon){
        $list .= '<i class="fa '.$accordion_icon.' fa-2x fa-fw"></i>">';
    }else{
        $list .= '<i class="fa fa-question-circle fa-2x fa-fw"></i>';
    }
    

    Note: You can only use the short way for 1 line. If your body statement exceeds 1 line you require the curly braces.

    You can also used something called the coditional operator, but I don't think that's what you're looking for, for now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵