簡體   English   中英

如何將我的WordPress網站主頁重定向到目標網頁,同時允許網站的其余部分不受影響?

[英]How do I redirect my WordPress site homepage to a landing page, while allowing the rest of the site to remain unaffected?

這是我在Wordpress網站上放置的簡單代碼:

<script type="text/javascript"> <!-- if (screen.width <= 800) {
window.location = "http://m.domain.com"; } //--> </script>

這個問題是,這會將我們網站的所有頁面重定向到目標網頁。 我只希望重定向主頁。 我怎么寫這個?

將此代碼放在主題的header.php中。

    <?php
    if (is_front_page()) {
    echo '<script type="text/javascript"> <!-- if (screen.width <= 800) {
    window.location = "http://m.domain.com"; } //--> </script>';
    }
    ?>

使用javascript重定向可能會導致您的網站受到Google的處罰。

使用PHP的Header重定向要好得多。 您可以根據用戶代理檢測移動用戶,而不僅僅依賴於瀏覽器窗口的寬度。

如果您無法自己編寫所有代碼,則此插件可能對此有所幫助。

https://wordpress.org/plugins/equivalent-mobile-redirect/

將此代碼放在主題header.php的頂部

<?php
    if(is_front_page())
        header('Location: http://example.com');

?>

這只會在首頁上重定向。 只需將example.com替換為您想要的網址即可。

要為不同但特定的頁面設置重定向,可以使用get_the_ID()。

例如

<?php
    if(is_front_page())
        header('Location: http://example.com');
    elseif(get_the_ID() == PAGE ID HERE)
        header('Location: http://example2.com');
    elseif(get_the_ID() == PAGE ID HERE)
        header('Location: http://example3.com');
?>

如果您想在所有頁面上重定向但仍然將主頁轉到其他位置並單獨保留一些頁面,請使用以下代碼。

<?php
    if(is_front_page())
        header('Location: http://example.com');
    elseif(get_the_ID() != PAGE ID HERE || get_the_ID() != 2ND PAGE ID HERE)
        header('Location: http://example2.com');
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM