I'm using this plugin to create a google product feed:
https://github.com/wp-plugins/purple-xmls-google-product-feed-for-woocommerce/blob/master/google-feeds.php
I want to output a custom value 'GTIN' that I've added to each product with Advanced Custom Fields... Plugin.... I'm trying to use the code from here:
http://www.advancedcustomfields.com/resources/getting-started/displaying-custom-field-values-in-your-theme/
But I'm guessing this isn't working (no error, but no value is returned) because I'm not inside the wordpress loop.
My goal is something like:
if the_field('gtin') != "" then $output.= ' <g:gtin>' . the_field('gtin') . '</g:gtin>' . PHP_EOL;
Thanks,
-Eric
Arnav Joy answers:
please try this
<?php
if ( get_field('gtin') != "" ) { $output.= ' <g:gtin>' . get_field('gtin') . '</g:gtin>' }
?>
Arnav Joy comments:
the_field() will print the value , you have to use get_field() for it to check and use it
please check
http://www.advancedcustomfields.com/resources/functions/get_field/
EricNeedsHelp comments:
Thanks, but get_field does not work in this plugin:
https://github.com/wp-plugins/purple-xmls-google-product-feed-for-woocommerce/blob/master/google-feeds.php
Arnav Joy comments:
please try this
<?php
$gtin = get_post_meta( $prod->ID,'gtin',true );
if ( $gtin ){ $output.= ' <g:gtin>' . $gtin . '</g:gtin>' . PHP_EOL;}
?>
EricNeedsHelp comments:
get_post_meta() works, thanks!
Arnav Joy comments:
good to hear , please vote and close the question.
Dbranes answers:
You can try this:
$gtin = get_field( 'gtin', $prod->ID);
if( ! empty( $gtin )){
$output .= '<g:gtin>' . $gtin . '</g:gtin>' . PHP_EOL;
}
or try this instead:
$gtin = get_post_meta( $prod->ID, 'gtin', TRUE );
if( ! empty( $gtin )){
$output .= sprintf( '<g:gtin>%d</g:gtin>', $gtin ) . PHP_EOL;
}
if your string contains only numbers or like this for general strings:
$output .= sprintf( '<g:gtin>%s</g:gtin>', $gtin ) . PHP_EOL;
Dbranes comments:
What kind of information does the field <em>gtin</em> contain ?
EricNeedsHelp comments:
Thanks, but get_field does not work in this plugin:
https://github.com/wp-plugins/purple-xmls-google-product-feed-for-woocommerce/blob/master/google-feeds.php
EricNeedsHelp comments:
gtin contains a string like 000123324
Dbranes comments:
Does <em>get_post_meta()</em> work instead, see updated answer?
EricNeedsHelp comments:
get_post_meta() works, thanks!