Joomla 3,将 php else if for site link 更改为全局

Joomla 3 , change php else if for site link to something global

我有一个 php elseif 代码,看起来像那样,

        <?php if ($item->getPrimaryImage()) :?>
        <img src="<?php echo $item->getPrimaryImage()->getSource(); ?>" class="sprocket-lists-image" alt="" />
        <?php endif; ?>
        <?php echo $item->getText(); ?>
        <?php if ($item->getPrimaryLink()) : ?></br>

        <?php if ($item->getPrimaryLink()->getUrl() == "index.php/component/content/article?id=3200") : ?></br> 
        <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>

        <?php elseif ($item->getPrimaryLink()->getUrl() == "index.php/component/content/article?id=1508") : ?></br>            
         <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>

        <?php elseif ($item->getPrimaryLink()->getUrl() == "index.php/component/content/article?id=1840") : ?></br>            
         <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a> 

        <?php elseif ($item->getPrimaryLink()->getUrl() == "index.php/component/content/article?id=2541") : ?></br>            
         <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>    

        <?php elseif ($item->getPrimaryLink()->getUrl() == "index.php/topmenu-1478/3350-2015-03-10-09-52-10") : ?></br>            
        <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>       

        <?php elseif ($item->getPrimaryLink()->getUrl() == "index.php/component/content/article?id=3426") : ?></br>            
        <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>      

        <?php elseif ($item->getPrimaryLink()->getUrl() == "index.php/component/content/article?id=3420") : ?></br>            
        <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>                  

        <?php else : ?></br>

        <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="modal" rel="{size: {x: 1024, y: 550}, handler:'iframe'}"><span><?php rc_e('READ_MORE'); ?></span></a>       

        <?php endif; ?>
        <?php endif; ?>

代码很简单,就是

  1. 所有 elseif 都有一个 link 开头,例如 == "index.php... 等等,这意味着阅读按钮将在网站内部打开,如 _parent link 没有模态..

  2. 最后一个(案例)else open all the rest links INSIDE THE MODAL

问题

您会注意到 elseif 案例中的所有 link 都以“index.php/.., 是否有任何编码方式可以避免所有 elseif 并具有全局性,这意味着可以识别起始 link "index.php/.. ? 您会注意到 elseif

之后的操作
  <a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" class="readon"><span><?php rc_e('READ_MORE'); ?></span></a>

在所有情况下都是一样的。

我需要一段代码来理解-识别 link 开头的“index.php/.. 这可能吗?

目标

我的目标是我不想在每次必须添加新的 link 时都使用这么多 elseif,代码会处理它,因为现在每次我必须添加一个新的图像横幅,它将 link 到 joomla 网站内的一篇文章我必须硬编码并添加一个新的 elseif 像上面一样,否则它将在模态中打开它..

我希望它足够清楚,请提供一些帮助,因为我已经尝试了很长时间来解决这个问题..

提前感谢您阅读本文..

使用像 http://url.thephpleague.com/3.0/ 这样的 URL 库应该可以在 :

之间分解 URL

index.php/component/content/article

和 GET 参数 id。

然后你可以简化你的代码。

检查字符串是否以特定单词开头的通用 if 子句是:

<?php if(substr($item->getPrimaryLink()->getUrl(), 0, 10) == "index.php/") : ?>
...
<?php endif; ?>

参考:substr