Forums

Tricky if/else statement. Is it possible? (16 posts)

  1. jessn
    Member
    Posted 2 years ago #

    I'm using the Global Translator plugin to help translate a Spanish website into English for some users. However in the header I'm pulling in an image that has some Spanish wording, and I would like to switch that image to an English one for the English translated pages...

    The regular website URL reads http://www.mysite.com/about/ and the English translated version reads http://www.mysite.com/en/about/

    I'm wondering if there is some sort of if/else statement that I can write that would say "if the domain name has a /en/ in it, use this image"

    Anyone have any ideas? Thanks for any help!

  2. vtxyzzy
    Member
    Posted 2 years ago #

    This might be what you want:

    <?php
    if ( false === strpos(get_bloginfo('url'), '/en/') ) {
      // Use the Spanish image
    } else {
      // Use the English image
    }
    ?>
  3. jessn
    Member
    Posted 2 years ago #

    Thanks! That seems like it should work. Here's how I have it typed out:

    <?php
    if ( false === strpos(get_bloginfo('url'), '/en/') ) { ?>
       <a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logoBlue.png" alt="logo" class="logo" /></a>
    <? } else { ?>
    <a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/logoBlueen.png" alt="logo" class="logo" /></a>
    <? } ?>

    And you can see it on this page: http://www.milliemunoz.com/contacto/
    English version: http://www.milliemunoz.com/en/contacto/

    There are some little flags in the sidebar. However clicking on the english version doesn't change the header...any idea why?

  4. vtxyzzy
    Member
    Posted 2 years ago #

    After thinking about it a while, I don't think that the code I posted can work because the blog url will always be the same for all pages. I think you will need the permalink to the particular page.

    A quick not-so-pretty solution would be to use the page IDs in the if-test until a better way can be found.

  5. vtxyzzy
    Member
    Posted 2 years ago #

    This may work:

    if ( false === strpos($_SERVER['REQUEST_URI'], '/en/') ) { ?>

  6. jessn
    Member
    Posted 2 years ago #

    Thanks so much for your help! It didn't work, but I appreciate your assistance!

  7. vtxyzzy
    Member
    Posted 2 years ago #

    Just as a check, try this to see if you can force the English image:

    if ( false ) { ?>

    If that doesn't work, then it is not the if-test that is the problem.
    `

  8. jessn
    Member
    Posted 2 years ago #

    Ok if I change it to if ( false ) { ?> then it shows the second image.

    if ( false ) { ?>
    <a href="<?php bloginfo('url'); ?>"><img src="spanish image" alt="logo" class="logo" /></a>
    <? } else { ?>
    <a href="<?php bloginfo('url'); ?>"><img src="english image" alt="logo" class="logo" /></a>
    <? } ?>

    Is that a good thing?

  9. vtxyzzy
    Member
    Posted 2 years ago #

    Well, it tells us that the correct image will be shown if we can get the if-test right.

    I would like to know the value of $_SERVER['REQUEST_URI'] on the two pages. Can you put this line of code in the php file that displays the pages so we can see what is there?

    <?php echo '<p>REQUEST:';print_r($_SERVER['REQUEST_URI']); echo '</p>'; ?>

    Ideally, the code should go just ahead of the loop. If this is not clear, please let me know and I will try to be more explicit.

  10. jessn
    Member
    Posted 2 years ago #

    Got it up, thanks. Ok so if I'm on the home page it says "REQUEST:/ " (spanish page). And right now the English version of the home page is cached, so it's not showing me the text. But I can see it on the English about page.

    This url: http://www.milliemunoz.com/en/about/
    Prints out: REQUEST: / about /

  11. vtxyzzy
    Member
    Posted 2 years ago #

    OK, there is no '/en/' in the REQUEST, so we can't use that to tell if we are on the Englixh page. Is there any other way that the REQUESTs are different? Any ideas on other ways to detect which page we are on?

  12. jessn
    Member
    Posted 2 years ago #

    Well hm...this is my first time using this plugin so I'm not sure. I'll Google around though and see if I can find anything. Thank you again for your help!!

  13. Darrell Schauss
    Member
    Posted 2 years ago #

    Try echo $_SERVER['REQUEST_URI']; instead of print_r. If this verifies the REQUEST_URI has /en/ then try..

    if(preg_match("|\/en\/|",$_SERVER['REQUEST_URI'])){
    //has EN
    }else{
    //not EN
    }
  14. vtxyzzy
    Member
    Posted 2 years ago #

    I think for a simple variable print_r and echo will give the same results.

    print_r will just give structure information if the variable is an object or an array.

  15. Darrell Schauss
    Member
    Posted 2 years ago #

    On my default install of wordpress with permalinks my REQUEST_URI has /en/about/ in it (with echo or print_r). It is weird yours is only printing about with the spacing in the slashes. It could be something to do with your host maybe?

  16. jessn
    Member
    Posted 2 years ago #

    Hm good to know dralezero! Thanks- I'll check with my host and see what they say!

Topic Closed

This topic has been closed to new replies.

About this Topic