I’ve got a custom post type – ID productpopup– which I want to hide the admin bar for all users including admin. The theory behind my function is as follows:

The post type is selected using

get_post_type( $post ) == 'productpopup'

And then the Admin Bar is hidden using

add_filter( 'show_admin_bar', '__return_false' );

So putting the following in my themes functions.php in my mind should work, but it doesn’t

if ( get_post_type( $post ) == 'productpopup' )
add_filter( 'show_admin_bar', '__return_false' );

WordPress 4.3.1

1
1

Don’t hack your WordPress core. It’s overriden after every upgrade (plugins do exist for some reason).

You can solve your problem in this way:

1) Open your single.php.
2) Define

<?php 
function hideAdminBar ($post_id)
{
    if (get_post_type ($post_id) == 'post')
    {
        add_filter ('show_admin_bar', '__return_false');

        /* For removing the top blank space. */
        echo '<style type="text/css" media="screen">
            html { margin-top: 0px !important; }
            * html body { margin-top: 0px !important; }
            </style>';
    }
} 
?> 

3) Inside The Loop, call this function right after the while condition. Like this:

<?php while ( have_posts() ) : the_post();?>

    <?php hideAdminBar (get_the_ID ()); ?>
    /* etc. */

<?php endwhile; ?>

Hope this solves your issue.

Leave a Reply

Your email address will not be published. Required fields are marked *