• Resolved mariostella

    (@mariostella)


    Hi, the function <?php $mysiteurl = get_option(‘site_url’); ?> used to work like a charm, but all of the sudden it spits out non existing URLs. The matter of fact is that it does not work only when the function is called in a series of if statements like this:

    <?php
    $test = get_post_meta($post->ID, 'whatever', true);
    if ($test=="10")
    echo "<a href='<?php echo $mysiteurl; ?>/a-page.html'>10</a>";
    elseif($test=="11")
    echo "<a href='<?php echo $mysiteurl; ?>/another-page.html'>11</a>";
    ?>

    As I said it used to work fine, but all of a sudden it includes
    <?php echo ; ?> right before /a-page.html or /another-page.html if it’s on the home page. If it’s in a post page it also includes the slug with the portion relative to the title of the post between <?php echo ; ?> and the URL of the site.

    So if it’s in the home what should be https://mysite.com/a-page.html turns into https://mysite.com/&lt;?php echo ; ?>/a-page.html

    If it’s in a post page it becomes https://mysite.com/post-title/&lt;?php echo ; ?>/a-page.html

    Needless to say this messes up all the links in the conditional statements.
    What could it depend on?
    Thanks in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • You have nested php tags (the one containing echo $mysiteurl), which is a problem.

    Because using echo for html in an if, elseif, else is tricky, I’d recommend this format instead:

    <?php $test = get_post_meta($post->ID, 'whatever', true); ?>
    
    <?php if ($test=="10") { ?>
    <!-- code here -->
    
    <?php } elseif ($test=="11") { ?>
    <!-- code here -->
    
    <?php } else { ?>
    <!-- code here -->
    
    <?php } ?>
    Thread Starter mariostella

    (@mariostella)

    Thanks Iridiax! That worked great! I had a hint it was the syntax, but I would not have known how to rewrite it. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_option(‘site_url’); not working anymore’ is closed to new replies.