4

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

4

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.

  • I'm not 'hacking' the Wordpress core - that was going in my themes functions.php - sorry for confusion! – CalvT Oct 1 '15 at 16:41
  • It works thanks :) Only thing - it still leaves the space where the admin bar was blank - any ideas how to remove this? – CalvT Oct 1 '15 at 16:42
  • I modified the function hideAdminBar() so that it also prints the required CSS for removing the blank space. If sometime I find a more elegant workaround for this I will tell you :) – Daniel Muñoz Parsapoormoghadam Oct 1 '15 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.