Custom Field Conditional For Sidebars

Hi all,

I’m wanting to have a custom true/false field (using ACF) to allow
showing or hiding sidebars for a page. I’ve got the field setup on
pages, but cannot get it to work in the lib/config.php display_sidebar
function:

/**
 * Define which pages shouldn't have the sidebar
 */
function display_sidebar() {
  static $display;

  if (!isset($display)) {
    $conditionalCheck = new Sage\ConditionalTagCheck(
      /**
       * Any of these conditional tags that return true won't show the sidebar.
       * You can also specify your own custom function as long as it returns a boolean.
       *
       * To use a function that accepts arguments, use an array instead of just the function name as a string.
       *
       * Examples:
       *
       * 'is_single'
       * 'is_archive'
       * ['is_page', 'about-me']
       * ['is_tax', ['flavor', 'mild']]
       * ['is_page_template', 'about.php']
       * ['is_post_type_archive', ['foo', 'bar', 'baz']]
       *
       */
      [
        'is_404',
        'is_front_page',
        ['is_page_template', 'template-custom.php'],
        if(get_field('hide_sidebar') {
           return true;
        }
      ]
    );

   $display = apply_filters('sage/display_sidebar', $conditionalCheck->result);
  }

  return $display;
}

I know I’m doing something wrong, can anyone shed some light please?

Please take a look at the comment, it accepts strings, not conditional statements. I assume you’re getting some type of error on the page saying something to that effect.

You can either set up your own function with the if(get_field('hide_sidebar') conditional, or else you can filter sage/display_sidebar for more direct results.

1 Like

You could also purchase my plugin, which includes the same functionality and quite a bit more.

1 Like