Home › Forums › Front-end Issues › Can't see custom fields on Woocommerce shop page
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
You main shop page is a type of archive page, where are you trying to get this value from?
$post_object = get_field('select_sidebar');
Is it on a specific page/product? Is this inside of some type of a WP post loop while(have_posts)
?
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.
Thanks
Should have been more clear. I mean, where are the fields located? On an options page? On a product “post”. Since the “shop” page is a WC product archive page then you need to supply the post ID that the fields are saved to.
@hube2 The fields are located in a custom post type called “sidebars”, and you can pull in any one of these, so how do I get the ID of the sidebar that’s been chosen?
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
global $post;
before $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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.