WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to hide/remove sidebar on certain posts (not pages)? (3 posts)

  1. astriv
    Member
    Posted 2 years ago #

    I tried adding this code in header.php before </head> but nothing happened:

    <?php // if page has custom field called disableSidebar = true, remove sidebar
    $disableSidebar = get_post_meta($post->ID, ‘disableSidebar’, $single = true);
    if ($disableSidebar !== ‘true’) { get_sidebar(); }
    ?>
    <?php if ( !( is_page(array('10, 16') ) ) ) { ?>
    <?php get_sidebar(); ?>
    <?php } ?>

    http://showfreetv.com

  2. Rescuework Support
    Member
    Posted 2 years ago #

    Please check whether $disableSidebar has a correct data or not.

    <?php // if page has custom field called disableSidebar = true, remove sidebar
    $disableSidebar = get_post_meta($post->ID, ‘disableSidebar’, $single = true);
    var_dump($disableSidebar); // added here
    if ($disableSidebar !== ‘true’) { get_sidebar(); }
    ?>
    <?php if ( !( is_page(array('10, 16') ) ) ) { ?>
    <?php get_sidebar(); ?>
    <?php } ?>

    This code does not solve the problem, but may provide a hint to solve it.

  3. katmac_aus
    Member
    Posted 2 years ago #

    You're called that outside of the loop so you need to run wp_query first. Try adding in:

    <?php  global $wp_query; ?>
    <?php $thePostID = $wp_query->post->ID; ?>
    <?php $disableSidebar = get_post_meta($thePostID, ‘disableSidebar’, $single = true); ?>
    etc...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags