Php代码在href =" "

时间:2017-12-23 23:50:37

标签: php hyperlink href

我想在href =""中添加PHP代码。 ,但我的网站崩溃了。 我尝试了什么,我想检查内部网站URL是否有字符串'en'来更改a href内的路径,但是我的网站崩溃了!这是在wordpress中,但我认为它是纯PHP。

<ul>
    <li><a href=
            <?php
                $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

                if (strpos($url,'en') !== false) {
                    echo ("en/about" );
                } elseif{
                    echo ("/o-nama" );
                } 
            ?>
        >
            <?php _e('About Distillery','all_custom_strings'); ?> 
        </a>
    </li>
</ul>

1 个答案:

答案 0 :(得分:0)

像这样简单:

<ul>
    <li>
        <?php
        $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        switch (true) {
            case strpos($_SERVER['REQUEST_URI'], 'en') !== false:
                $action = '/en/about';
                break;
            case strpos($_SERVER['REQUEST_URI'], 'ru') !== false:
                $action =  ''; // whatever you set for 'ru'
                break;
            default:
                $action =  '/o-nama';
        }
        ?>
        <a href="<?php echo $action; ?>">
            <?php _e('About Distillery','all_custom_strings'); ?> 
        </a>
    </li>
</ul>

在你的代码中,如果没有条件,你就有了。