根据条件更改页面内容 Wordpress

Change page content based on condition Wordpress

我想根据登录用户的角色更改为 wordpress 网站的主页。

对于简单的情况(即用户是否登录),我尝试使用函数 is_user_logged_in()

代码如下:

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";
            }

但问题是它改变了每一页的内容。我只想对特定页面执行此操作..(即主页)。我怎么做?任何帮助将不胜感激。

要做到这一点,您必须添加其他条件,例如对于主页:

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

...

}

其他类型的内容也类似:is_page()is_category()is_author() 等。查看文档 here.

检查页面的 ID 并将其与您想要的特定页面进行比较 您也可以为要更改的页面设置类别并检查该类别