VIVEK SEDANI
VIVEK SEDANI

Reputation: 407

Change page content based on condition Wordpress

I would like to change to homepage of a wordpress website depending on the role of the user who is logged in.

For the simple case ( i.e. whether a user is logged in or not ) I have tried using the function is_user_logged_in()

the code is as below:

if(!is_user_logged_in()){
                while ( have_posts() ) : the_post();
                    // Include the page content template.
                    get_template_part( 'content', 'page' );
                    // If comments are open or we have at least one comment, load up the comment template.
                    if ( comments_open() || get_comments_number() ) {
                        comments_template();
                    }
                endwhile;
            }else{
                echo "you are logged in";
            }

But the problem is that it changes the content in the each and every page. I would like to do that for a particular page only.. ( i.e. homepage ). How do i do that? Any help would be greatly appreciated.

Upvotes: 0

Views: 1058

Answers (2)

Jogi
Jogi

Reputation: 1

check for the Id of the page and compare it to your wanted specific page also you could set a category for the pages you want to alter and check for that

Upvotes: -2

Grzegorz Pawlik
Grzegorz Pawlik

Reputation: 2216

To do it you would have to add another condition(s), e.g. for the homepage:

if(!is_user_logged_in() && is_home()){

...

}

And similarly for the other type of the content: is_page(), is_category(), is_author(), etc. Check out the docs here.

Upvotes: 0

Related Questions