Different WordPress front page for logged out and

2019-05-09 23:29发布

The WordPress reading settings can be found under Settings > Reading.

I have the WordPress 'Front page displays' option set to 'Static front page'.

My 'Front page' is set to 'About'.

I am trying to have a different front page for logged out and logged in users. Currently everyone views the 'About' page when they visit example.com.

  • If I visit example.com when logged out I want to see my 'about page'
  • If I visit example.com when logged in I want to see my 'contact page'

Does anybody know how I can achieve this?

标签: php wordpress
6条回答
Root(大扎)
2楼-- · 2019-05-10 00:01
  • Edit: Didn't see your comment before I posted my answer.

You could add this to your functions.php

add_action('init', 'my_redirector');

function my_redirector(){
    if( !is_user_logged_in() ) {
        wp_redirect( 'http://www.example.com/contact_us'); 
        exit;
    }
}

It's not a great solution to hard code in your url, but you could pick up the URL in other ways.

查看更多
戒情不戒烟
3楼-- · 2019-05-10 00:01

UserPro plugin gets this done easily. Just enter the URL you want logged in users to see instead of the normal homepage. So Simple!

查看更多
爷的心禁止访问
4楼-- · 2019-05-10 00:06

In the body of your page-about.php (or which ever template file the About page is using) you just need to use a conditional statement.

<?php if( is_user_logged_in() ) { ?>


content for logged in users


<?php } else { ?>


content for non logged in users


<?php } ?>
查看更多
可以哭但决不认输i
5楼-- · 2019-05-10 00:09

It's possible to set the front page programmatically but not sure if this is a solution to your question, so, paste this code in your functions.php file and give it a try

if( is_user_logged_in() ) {
    $page = get_page_by_title( 'Contact Me');
    update_option( 'page_on_front', $page->ID );
    update_option( 'show_on_front', 'page' );
}
else {
    $page = get_page_by_title( 'About Me' );
    update_option( 'page_on_front', $page->ID );
    update_option( 'show_on_front', 'page' );
}

P/S: Not tested, just try it and please response what you get.

查看更多
可以哭但决不认输i
6楼-- · 2019-05-10 00:11

Try this plugin recently released to solve the problem head on https://wordpress.org/plugins/dual-homepage/. It’s solves the problem by allowing you the user to select a homepage for logged out users and another for logged in users.

What may also help is that with the newly updated Wordpress admin post interface using blocks you can set some for logged in users to see and set others for logged out users to see. I use a combination of this plugin and the new Wordpress blocks to achieve what I wanted with my site. To get started you need to download “Block Options” first which you can get it on the plugin directory.

查看更多
戒情不戒烟
7楼-- · 2019-05-10 00:16

For anyone else that has this problem you could just add this to the header.php, at the very top before the <!DOCTYPE html>:

<?php if(is_front_page()) {
    if (is_user_logged_in()) {
        $newURL = 'http://YourWebsiteURLhere.com';
        header('Location: '.$newURL);
    }
} ?>

OR you can add this to Functions:

function homepage_template_redirect()
{
    if( is_front_page() && is_user_logged_in() )
    {
        wp_redirect(get_page_link('contact'));
        exit();
    }
}
add_action( 'template_redirect', 'homepage_template_redirect' );
查看更多
登录 后发表回答