Support

Account

Home Forums ACF PRO Choose sidebar for page with checkbox field

Solved

Choose sidebar for page with checkbox field

  • Hi.

    i have 3 sidebars (siebar-1, sidebar-2, sidebar-3)

    on page.php i have the following code: (here the sidebar should load)

    
    ...
    	<div class="col-lg-3 col-lg-offset-1">
    		<?php 
    		$sidebar = get_field('sidebar');
    		dynamic_sidebar($sidebar);
    		?>
    	</div>
    ...
    

    for pages i have the following acf: (return value)

    Checkbox field with this options:

    sidebar-1 : Sidebar Strom
    sidebar-2 : Sidebar Gas
    sidebar-3 : Sidebar Wärme

    And i got the following error (frontend):
    Warning: preg_match() expects parameter 2 to be string, array given in /wp-includes/formatting.php on line 1515

    Warning: strip_tags() expects parameter 1 to be string, array given in /wp-includes/formatting.php on line 1967

    Can not find my fault.

  • Looking at this I would guess that you’re returning “Both (Array)”. You need to either set the field to only return the value or use

    
    $sidebar = get_field('sidebar');
    dynamic_sidebar($sidebar['value']);

    `

  • the checkbox field returned “value” but it doesnt work either…

    Maybe because the snippet on page.php is outside the loop?

    
    <div class="row">
    	<div class="col-lg-8 content-area">
    		<main id="main" class="site-main">
    
    			<?php
    			while ( have_posts() ) : the_post();
    
    				get_template_part( 'template-parts/content', 'page' );
    
    			endwhile; // End of the loop.
    			?>
    
    		</main><!-- #main -->
    	</div>
    	<div class="col-lg-3 col-lg-offset-1">
    		<?php 
    		$sidebar = get_field('sidebar');
    		dynamic_sidebar($sidebar);
    		?>
    		test
    	</div>
    </div>
  • More than likely, yes, you will need to get the post id and supply that in the ACF function. If this is on a single post template then it’s not too hard

    
    // get the current post id
    $queried_object = get_queried_object();
    $post_id = $queried_object->ID;
    

    then when you get the field

    
    $sidebar = get_field('sidebar', $post_id);
    
  • The Answer:

    I have put the Code inside the page loop and i have to change the code a little bit because the field type is checkbox…

    <?php
    
    	$sidebars = get_field('sidebar');
    
    	if( $sidebars ): 
    		?>
    		
    			<?php foreach( $sidebars as $sidebar ): ?>
    				
    					<?php dynamic_sidebar($sidebar); ?>
    				
    			<?php endforeach; ?>
    	<?php endif; ?>
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.