正如您在评论中提到的,如果最适合您的方式是根据用户是否登录显示不同的头文件,那么这是实现此目的的最佳方式:
if( is_user_logged_in() ) :
/** This is the name of your second header file.
* It assumes the \'header-\' and the \'.php\' portions.
* So the following get_header would use this file \'header-loggedin.php\'. */
get_header( \'loggedin\' );
else :
/* This would use the default header for your child theme named \'header.php\'. */
get_header();
endif;
现在,如果导航不同,那么我要做的就是在标题中创建两个不同的导航位置,并根据用户是否登录有条件地加载它们。然后,在WP的菜单管理区域中,您可以管理两个不同的菜单,只需将其中一个指定为“主登录”和“主未登录”或类似的内容。
最好的解决方案实际上只取决于您实际需要多少不同方面的标题。如果只是导航,那么我会选择条件菜单位置。如果不仅仅是导航,我会选择条件头加载。
下面是创建两个菜单位置的方法,将其添加到函数中。php:
register_nav_menus(
array(
/* Make sure you change the textdomain to match yor child themes. */
\'header-loggedin\' => esc_html__( \'Main Menu Logged In\', \'tetdomain\' ),
\'header-loggedout\' => esc_html__( \'Main Menu Logged Out\', \'textdomain\' )
)
);
这将创建两个新的菜单位置,您可以在WP中为其指定菜单
Appearances -> Menus screen.下一步,如果选择单曲header.php 文件中,您将以下代码放置在导航应该显示的位置:
if( is_user_logged_in() ) :
wp_nav_menu(
array(
\'theme_location\' => \'header-loggedin\',
\'menu_id\' => \'header_loggedin\'
)
);
else :
wp_nav_menu(
array(
\'theme_location\' => \'header-loggedout\',
\'menu_id\' => \'header_loggedout\'
)
);
endif;
如您所见,它使用相同的
is_user_logged_in() 检查,然后根据条件的结果简单地确定应该使用哪个“菜单位置”。
wp_nav_menu 还有很多可配置的选项,所以如果您想自定义容器等,请仔细考虑。