在WordPress中使用第一篇文章作为开始页面

时间:2009-12-18 14:01:53

标签: wordpress redirect

当用户转到WordPress安装的主URL时,是否有一种简单的方法可以将用户重定向到第一篇文章...就像,如果用户访问domain.com,他们将被重定向到domain.com/title-of-the-first-post

3 个答案:

答案 0 :(得分:1)

克里斯·霍尔姆斯做出重定向的回答很好。

如果htaccess不是一个选项,或者您想在WordPress中执行此操作,则可以使用is_home_page()和wp_redirect()向index.php添加简单逻辑。 http://codex.wordpress.org/Function_Reference/is_home http://codex.wordpress.org/Function_Reference/wp_redirect

如果你真的不想编写解决方案,我想这对https://superuser.com/来说是一个很好的问题 也许,您正在寻找http://codex.wordpress.org/Creating_a_Static_Front_Page

答案 1 :(得分:1)

经过几天的尝试和失败后,我能够构建以下解决方案:

将以下代码添加到位于 wp-content / themes / nameOfTheme 中的 header.php 文件中。确保没有空格或在开场前输入

<?php
if(is_home()) {
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=1');
    while ($recentPosts->have_posts()) : $recentPosts->the_post();
    wp_redirect(get_permalink(), 301);
    endwhile;
}
?>

答案 2 :(得分:0)

使用.htaccess文件并执行redirect