Wordpress:使用method ="post"进行多语言选择

Rob*_*Rob 6 html php wordpress multilingual

我正在构建一个具有双语言和两个标志作为入口页面的网站.我打算在<form method="post">旗帜周围使用,以便用户可以选择他们想要的语言.

然后在以下页面中我想使用类似的东西:

<?php
if( $_POST['language']=='uk' ){
    echo $uk;
}elseif( $_POST['language']=='french' ){
    echo $french;}
?>
Run Code Online (Sandbox Code Playgroud)

所以在点击标志时,他们选择了他们想要的语言.这只会在他们点击旗帜后才能在下一页上工作,还是可以继续导航到不同的页面,它仍然会选择选择的语言?

如果这不起作用,怎么办呢?


更新:

在我使用Wordpress之前,我认为我没有说清楚,显然不喜欢$_SESSION.

我在名为region.php的模板上有这个提交语言选择:

    <form action="<?php the_permalink(); ?>/home" name="region" method="post">              
        <div id="uk">
            <a href="javascript:document.region.submit()" name="UK">
                <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/-uk.png" width="259" height="160" alt="UK" />
            </a>
            <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1>
        </div>

        <div id="world">
            <a href="javascript:document.region.submit()" name="World">
                <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/
world.png" width="258" height="160" alt="
Rest of the World" />
            </a>
            <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1>                    
            </div>  
    </form>
Run Code Online (Sandbox Code Playgroud)

我需要在每个其他模板上放置什么来检查选择了哪种语言?如果英国已经被选中,那么它可以回应"英国",如果世界其他地方被选中则可以显示"世界".

这需要在多个页面上工作,因此如果他们转到about页面检查语言,那么如果他们导航到联系页面,它会再次检查语言 - 所有这些都必须来自初始语言选择.

Flu*_*feh 15

我会把选择放到$_SESSION变量中.这将留在他们身边,直到他们离开.你也可以很好地使用一个cookie.实际上,两者的组合会很棒,它会阻止那些不允许cookie访问访问的用户,而拥有cookie的人只需要选择一次.

编辑:工作代码示例:

<form action="<?php the_permalink(); ?>/home" name="region" method="post">              
    <div id="uk">
    <a href="javascript:document.region.submit()" name="UK">
        <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/=uk.png" width="259" height="160" alt="UK" />
    </a>
    <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1>
    </div>

    <div id="world">
    <a href="javascript:document.region.submit()" name="World">
        <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/world.png" width="258" height="160" alt="Rest of the World" />
    </a>
    <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1>                    
    </div>  
</form>
// I assume that this form sends a POST request to the following page URL.
Run Code Online (Sandbox Code Playgroud)

表单重定向到的页面:

<?php

    session_start();
    if(isset($_POST['country'])) 
    // assuming that your form returns a field called 'country'...
    {
        $_SESSION['myCountry'] = htmlspecialchars($_POST['country']);
        // Assumes that myCountry is now 'UK' or 'World'...
    }
    header('Location: http://www.example.com/yourIndexPage.php');
    // You want to use a quick snippet to process the form and 
    // then redirect them away. This makes for less stupid BACK actions
    // in the browser as well as data being resent to the code.
?>
Run Code Online (Sandbox Code Playgroud)

yourIndexpage.php

<?php

    // As the $_SESSION is now set, we can use it in the page as follows:
    session_start();
    switch($_SESSION['myCountry'])
    {
        case 'UK':
            echo $UK;
            // And anything else you want to do with the UK language.
            break;
        case 'World':
            echo $world;
            // etc etc etc...
            break;
        default:
            // echo out default stuff, probably 'World' at a guess.
            break;
    }

?>
Run Code Online (Sandbox Code Playgroud)

如果您使用wordpress,您应该阅读此内容.


Fra*_*llo 2

答案

首先,要向 WordPress 站点添加区域/多语言支持,没有比这个插件更好的选择了: http: //wpml.org/。它有与之相关的成本,但并不令人望而却步(您需要为出色的支持付费)。

其次,默认情况下不能使用$_SESSION。WP 在设计上是无状态的。也就是说,网上有大量的插件和教程可以获取此功能。

代码的东西

您的原始表单 html 没有输入。这是一张没有提交任何内容的表格。该表单有两个名为相同内容的提交location。因此,无论单击哪个按钮都会根据$_POST['location'].

<form action="<?php the_permalink(); ?>/home" name="region" method="post">              
    <div id="uk">
        <input type="submit" name="location" value="UK" />
        <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/uk.png" width="259" height="160" alt="UK" />
        <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1>
    </div>

    <div id="world">
        <input type="submit" name="location" value="World" />
        <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/world.png" width="258" height="160" alt="Rest of the World" />
        <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1>                    
    </div>  
</form>
Run Code Online (Sandbox Code Playgroud)

编写一个 wordpress 操作来处理帖子。将其添加到主题的函数文件中。查看http://php.net/manual/en/function.setcookie.php获取 setcookie 文档。

function set_location_cookie()
{
    if(isset($_POST['location']))
    {
        setcookie('location', $_POST['location'], time()+1209600);
    }
}
add_action('init', 'set_location_cookie');
Run Code Online (Sandbox Code Playgroud)

然后,在您想知道位置的任何地方:

<?php echo $_COOKIE["location"]; ?>
Run Code Online (Sandbox Code Playgroud)

附加信息

我最初单击“英国”开始,这设置了 cookie,然后我返回并单击“世界”,但这并没有复制世界的 cookie,它只是再次显示“英国”。每次做出不同的选择时是否可以擦除 cookie?

所以这是一个关于 cookie 在技术层面如何工作的问题:

当浏览器执行此操作[请求站点]时,它将在您的计算机上查找[您的站点]已设置的cookie文件。如果找到 cookie 文件,您的浏览器会将文件中的所有名称/值对连同 URL 一起发送到服务器。如果没有找到 cookie 文件,则不会发送 cookie 数据。

新的 cookie 数据最初不会发送,直到请求从浏览器发送到服务器后,新的 cookie 数据才会被保存并可与请求一起发送。

那么你如何让这项工作发挥作用呢? 成功设置 cookie 事件后重定向。

function set_location_cookie()
{
    if(isset($_POST['location']))
    {
        // Set Cookie
        setcookie('location', $_POST['location'], time()+1209600);
        // Reload the current page so that the cookie is sent with the request
        header('Location: '.$_SERVER['REQUEST_URI']);
    }
}
add_action('init', 'set_location_cookie');
Run Code Online (Sandbox Code Playgroud)

另外是否可以使用标志的图像作为提交按钮? 是的,使用 CSS 来设置输入按钮的样式。

为每个输入添加一个 id:id="uk-button"id="world-button"

CSS:

#uk-button {
    background-image: url(uk-flag.png);
    background-size: 100%;
    background-repeat:no-repeat;
}
#world-button {
    background-image: url(world-flag.png);
    background-size: 100%;
    background-repeat:no-repeat;
}
Run Code Online (Sandbox Code Playgroud)