0

I'm using woocommerce plugin in my wordpress web site. In Admin panel for products I've added few custom fields as shown bellow:

enter image description here

On my shop page, I want to print the custom field values, and I use the following code ( my code is inside the while loop):

 <?php echo get_post_meta(get_the_ID(), 'flavour1Title', true); ?>

But I get empty string for any retrieved value.

Please can someone explain me what is wrong with my code?

Thx!

2 Answers 2

1

Firstly, i would use a custom function in your child themes functions file rather than add the code directly to the template file.

You can use the WooCommerce conditional tags and hooks in your code.

echo get_post_meta( get_the_ID(), 'collection_text', true ); 
4
  • Hm I agree, but it is not working in functions.php niether!
    – Mr.M
    Jan 3, 2014 at 16:56
  • What exact code did you try in functions? Jan 3, 2014 at 17:03
  • I added action for function in my functions.php, and that function should return me get_post_meta(get_the_ID(), 'flavour1Title', true); custom field called flavour1Title
    – Mr.M
    Jan 6, 2014 at 10:30
  • I assume you wrapped it correctly in PHP tags? Jun 29, 2015 at 12:53
-2

It would be best to use Advanced Custom Fields to handle your custom field implementation. They offer a free and pro version.

You create your field groups, and have them display in a custom post type (in this case, it would be the WooCommerce custom post type).

You can then call a field while in the loop as simple as <?php the_field('hero_image'); ?>

Here is a full example:

    <?php while ( have_posts() ) : the_post(); ?>

        <h1><?php the_field('custom_title'); ?></h1>

        <img src="<?php the_field('hero_image'); ?>" />

        <p><?php the_content(); ?></p>

    <?php endwhile; // end of the loop. ?>

Source

1
  • ACF is a lot more user-friendly too.
    – Aibrean
    Jan 21, 2016 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.