Is there any obvious reason why the following shouldn’t work? I’m using it fine on normal pages, but on my main Woocommerce shop page, it doesn’t display anything…
<?php $post_object = get_field('select_sidebar'); if( $post_object ): // override $post $post = $post_object; setup_postdata( $post ); ?> <div class="col-xs-12 col-sm-4 sidebar"> <?php if( have_rows('sidebar_blocks') ): ?> <?php while( have_rows('sidebar_blocks') ): the_row(); // vars $width = get_sub_field('block_width'); $image = get_sub_field('block_bg_image'); $colour = get_sub_field('block_bg_colour'); $text = get_sub_field('block_text'); $link = get_sub_field('link'); ?> <div class="col-xs-6 col-sm-<?php echo $width; ?> block" style="background-image: url(<?php echo $image; ?>); background-color: <?php echo $colour; ?>; "> <?php if( $text ): ?> <span class="overlay"><?php echo $text; ?></span> <?php endif; ?> </div> <?php endwhile; ?> <?php endif; ?>
Thanks for the reply @hube2. I’m getting that value from a field in the backend that is a drop-down selector. This selects from a post type called ‘Sidebars’. The idea is that you can create any number of sidebars, add content into them, then add them onto any page from the drop-down. As I mentioned, this works 100% on any standard page outside of Woocommerce.
In answer to your second question, the example I pasted above is in the generic sidebar.php file which is pulled into all page templates. It’s not wrapped in any kind of post loop.
sorry for the long delay, you’re reply got burried. Anyway, the post id should already be set up by this
$post_object = get_field('select_sidebar'); if( $post_object ): // override $post $post = $post_object; // this set up the post data including the ID setup_postdata( $post ); ?> <div class="col-xs-12 col-sm-4 sidebar"> <?php if( have_rows('sidebar_blocks') ): ?>
If this is not working then I’m not sure what the problem is. It could be that the global $post is not defined. Try defining it by adding
$post = $post_object;
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.