Search code examples
phphyperlinkhref

Php code in href=" "


I would like to add PHP code inside a href="" , but my site crashed. What I tried, I would like to check if inside site URL have string 'en' to change path inside a href, but with this my site crashed! This is inside wordpress but I think it's pure 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>

Solution

  • Make it simple like this:

    <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>
    

    In your code you have elseif without condition.