最近有一个需求,需要将网站首页跳转到指定页面(如指定文章/页面),并非显示默认的文章列表。经过一段时间的测试终于解决问题,废话不多说,直接上干货!

WordPress 网站打开首页跳转指定文章教程:
在主题的 functions.php 文件底部添加:
add_action('template_redirect', 'redirect_homepage_to_post');
function redirect_homepage_to_post() {
if (is_home() && !is_paged()) {
$post_id = 123; // 指定文章ID
wp_redirect(get_permalink($post_id), 301);
exit;
}
}
WordPress 网站打开首页跳转指定页面教程:
在主题的 functions.php 文件底部添加:
add_action('template_redirect', 'redirect_homepage_to_custom_page');
function redirect_homepage_to_custom_page() {
if (is_home() && !is_paged()) {
$page_id = 456; // 替换为自定义页面的实际ID
wp_redirect(get_permalink($page_id), 301);
exit;
}
}
文章ID及页面ID打开WordPress后台文章或者页面列表即可查看,具体效果预览:https://txy.ink/,这个效果设置成了打开首页跳转指定页面,跳转指定文章效果类似。

