2013-09-22 27 views
2

我正嘗試將我的WordPress主頁自動重定向到最新文章。 目前我用斯賓塞卡梅倫建議現在將WordPress主頁重定向到最新文章

function redirect_homepage() { 
    if(! is_home() && ! is_front_page()) 
     return; 

    wp_redirect('http://homepage.com/article1', 301); 
    exit; 
} 

add_action('template_redirect', 'redirect_homepage'); 

重定向,如果我發佈第2條我想要的網頁自動連接到第2條沒有我調整的functions.php。

我不想讓用戶看到www.example.com但只看到文章,因此在訪問該頁面時總是會重定向到最新的文章。

但是:
我希望有仍然可以訪問www.example.com/article1(手動鍵入URL)的可能性,即使已經有www.example.com/article2

我怎麼能達到這個目標?

+0

爲什麼不只是顯示在第一篇文章你主頁作爲「特色文章」或什麼?讓您的主頁重定向對用戶體驗不利,因爲您延遲了頁面加載時間。 –

+0

在這種情況下不可能,我不希望用戶看到wordpress主題,但只能使用WordPress的某個插件。 – user2804436

+0

這仍然沒有任何意義...你設置的方式,沒有人會看到您的網站的主頁。 –

回答

0

答案是在Get the ID of the latest post:做一個簡單的查詢來獲得一個帖子(默認情況下按最新排序),然後抓住它的永久鏈接並重定向。

Don't put this type of code in functions.php,創建你自己的迷你插件來做到這一點。如果你想禁用這個,只需要關閉一個插件而不是編輯一個文件。

<?php 
/* Plugin Name: Redirect Homepage */ 

add_action('template_redirect', 'redirect_homepage'); 

function redirect_homepage() 
{ 
    if(! is_home() && ! is_front_page()) 
     return; 

    // Adjust to the desired post type 
    $latest = get_posts("post_type=post&numberposts=1"); 
    $permalink = get_permalink($latest[0]->ID); 

    wp_redirect($permalink, 301); 
    exit; 
} 
+0

感謝您幫助我brasofilo。老實說:我沒有使用迷你插件的經驗,後面跟着一個朋友的解決方案,它完美地適合我的場景。沒有延遲,沒有壓力。謝謝! – user2804436

+0

唯一需要的經驗是將代碼複製到PHP文件中,將其上傳到插件文件夾並激活。 – brasofilo

0

從朋友A液: 在模板文件夾替換的index.php下列要求:

<?php global $query_string; query_posts($query_string.'&posts_per_page=1'); ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php header('Location: '.get_permalink()); ?> 
<?php endwhile; ?> 

謝謝你幫我出

相關問題