我想在我网站的一个页面上隐藏一个特定的用户角色的特定div,我试图在我的主题的function.php文件中插入以下代码,但不幸的是它不起作用,你有任何建议吗?
<?php
$user = wp_get_current_user();
if ( in_array( 'shop_manager', $user->roles ) ) {
?>
<script>
jQuery(document).ready(function( $ ){
jQuery( "#hide" ).empty();
});
</script>
<?php
}
?>
functions. PHP用于向您的站点添加PHP函数。如果您想在主题中打印HTML/JS,则需要将其添加到主题文件(index.php或footer.php)
把这段代码放在你的主题文件中,它应该可以工作。
使用footer钩子注入你的css/js代码:
function your_function() {
$user = wp_get_current_user();
if ( in_array( 'shop_manager', $user->roles ) ) {
?>
<script>
jQuery(document).ready(function( $ ){
jQuery( "#hide" ).empty();
});
</script>
<?php
}
}
add_action( 'wp_footer', 'your_function' );
add_action( 'wp_head', 'callback_hide_div_onpage' ); // wordpress header action hook
function callback_hide_div_onpage() {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = $user->roles;
if($roles[0] == 'student_role'){ // role name -> Student Role
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-us' ){ // add specific page to hide a div using style
?>
<style>
/* hide div using styling css option or you can used script for this as per requirement */
.target_div{
display:none !important;
}
</style>
<?php }
}
}
}