Support

Account

Home Forums Front-end Issues Default values don't show on frontend

Solved

Default values don't show on frontend

  • I’m using acf alongside woocommerce and would like to show a piece of code only on certain product category pages.

    So far I’ve created a text area field group, with a default value (my custom code) to be shown on only certain post taxonomies. The default value is showing up fine within the edit product page but when I view the product frontend, the acf doesn’t show up.

    However if I edit the text area on the edit product page, it works… eg add a space and then delete it.

    Is there a way for this default value to activate on the frontend without the user having to touch it? (this is a website for a client and I want this to be setup for them without them having to touch anything.)

  • Hi @krisscross90

    Unfortunately, due to system restrictions, it is not possible to load in the default value into the front end. Only on the backend.

    Your ‘add a space and then delete it.’ idea is the same as just saving the post. This will save the data to the DB and then you can load it in the front end.

    Basically, without saving the post, the reference field from the value to the field object will not exist, therefore, on the front end when you use get_field, there is no way for ACF to know which field you want to load the default value from.

    Sorry, but this is not possible.

    Thanks
    E

  • Hi Elliot,

    Thanks for your reply!
    Would this be possible to do manually? I’ve been trying to get this to work for so long now, and thought I’d cracked it using the acf plugin.

    Basically I need to show a piece of code on certain woocommerce category pages, but I can’t use the conditional statement is_product_cat within the single product page template.

    If you could advise me on the manual code I would need to add, in order to show a custom field, maybe I could rework it so that it resolves my issue?

    Any help would be greatly appreciated!

  • Hi @krisscross90

    have you got any code which you are currently using to render the ACF value? I can take a look at it and show you how you can load the field object and then load the default value manually.

    Thanks
    E

  • This is the code that I need to load
    <div id="requestsample"><a class="fancybox button" href="#contact_form_pop">Request a Sample</a><div class="fancybox-hidden" style="display: none;"><div class="hentry" id="contact_form_pop" style="width: 600px; height: 500px; overflow-x: hidden"><?php echo do_shortcode( '[contact-form-7 id="269" title="Request Sample"]' ); ?></div></div></div>

    There’s also a shortcode from another plugin in there… will that affect things?

    I’ve also tried using things like:

    <?php if ( is_product_category() ) {
    if ( is_product_category( 'coving' ) ) {
    echo '<div id="requestsample"><a class="fancybox button" href="#contact_form_pop">Request a Sample</a><div class="fancybox-hidden" style="display: none;"><div class="hentry" id="contact_form_pop" style="width: 600px; height: 500px; overflow-x: hidden"><?php echo do_shortcode( '[contact-form-7 id="269" title="Request Sample"]' ); ?></div></div></div>';
    } elseif ( is_product_category( 'skirting-boards' ) ) {
    echo '<div id="requestsample"><a class="fancybox button" href="#contact_form_pop">Request a Sample</a><div class="fancybox-hidden" style="display: none;"><div class="hentry" id="contact_form_pop" style="width: 600px; height: 500px; overflow-x: hidden"><?php echo do_shortcode( '[contact-form-7 id="269" title="Request Sample"]' ); ?></div></div></div>';
    } elseif ( is_product_category( 'dado-rails' ) ) {
    echo '<div id="requestsample"><a class="fancybox button" href="#contact_form_pop">Request a Sample</a><div class="fancybox-hidden" style="display: none;"><div class="hentry" id="contact_form_pop" style="width: 600px; height: 500px; overflow-x: hidden"><?php echo do_shortcode( '[contact-form-7 id="269" title="Request Sample"]' ); ?></div></div></div>';
    } elseif ( is_product_category( 'indirect-lighting-by-orac' ) ) {
    echo '<div id="requestsample"><a class="fancybox button" href="#contact_form_pop">Request a Sample</a><div class="fancybox-hidden" style="display: none;"><div class="hentry" id="contact_form_pop" style="width: 600px; height: 500px; overflow-x: hidden"><?php echo do_shortcode( '[contact-form-7 id="269" title="Request Sample"]' ); ?></div></div></div>';
    } else {
    echo ' ';
    }
    
    }?>

    But it doesn’t work 🙁
    I don’t have any other code to load the acf… I’ve just set everything up within the plugin. Thanks for all your help!

  • Hi @krisscross90

    Looking at your code you have many syntax errors. The main of which is that you are using php tags within an echo string!!?

    Besides that, I can’t see any code to render ACF data. Have you read the docs yet? If not, can you please do so ASAP.

    Thanks
    E

  • Hi Elliot,

    Apologies! I don’t have any php knowledge. I was trying to follow some examples I found online, but obviously got it all wrong.
    I’ll take a look at the documentation now.
    Thanks for all your help!

    Is this the code you speak of to render acf data:
    <div id="requestsample"><?php the_field('request_sample'); ?></div>

  • Hi Elliot,

    I took another look at the default value set up. It works perfectly without touching the acf when you add a new product and I only had a few products set up so I just had to update those few. Thanks for all your help!

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Default values don't show on frontend’ is closed to new replies.