2

I've written a custom function which works with the native WordPress custom fields function but I need it to work with the Advanced Custom Fields plugin.

I have created the custom field and meta box using the plugin and added the function to my child theme's functions.php file.

I am using the field name (demo_field) for the meta box in the custom function but the meta box does not hook in to the function.

add_action( 'genesis_after_post_content', 'custom_field_before_content' );
function custom_field_before_content() {
  if(is_single() ) {
    genesis_custom_field('demo_field');
  }
}

What am I doing wrong?

Ok i fixed that but it doesn't return the image even though i configured the settings to return value for the image object.

It displays the HTML for the image.

add_action( 'genesis_after_post_title', 'custom_field_before_content' );
function custom_field_before_content() {
if(is_single() ) {
the_field('second_image');
  }
}

Here's what it displays:

32538, , second feature image, , , /wp-content/uploads/2013/06/second-feature-image.png, 600, 300, Array

Here's the final solution i worked out myself which i tested and works with Genesis only. You can change the hook if using another theme with hooks.

add_action( 'genesis_after_post_title', 'custom_field_before_content' );
function custom_field_before_content() {
if(is_single() ) 
if( get_field('second_image') ):
?><img src="<?php the_field('second_image'); ?>" alt="" /><?php
endif;

}

The code comes from the ACF website.http://www.advancedcustomfields.com/resources/field-types/image/

2 Answers 2

1

Any reason why you're using genesis_custom_field()? Advanced custom fields has its own output functions. If you want to echo the demo_field you can use the function the_field('demo_field'); or if you want to use the value or save it as a variable you can use $demo = get_field('demo_field');.

4
  • Ok i fixed that but it doesn't return the image even though i configured the settings toi return the image object.
    – Haymanpl
    Jun 5, 2013 at 14:21
  • The image object contains an array with all the image attributes, including the url. You should select image URL instead of image object and create the image like this: <img src="<?php the_field('demo_field'); ?>" alt="<?php the_title(); ?>"> Jun 6, 2013 at 12:44
  • How do i put that code into the custom function above which includes the genesis hook?
    – Haymanpl
    Jun 6, 2013 at 13:20
  • Your code is correct. As I mentioned in my last comment, select "Image URL" instead of "Image Object" in the field options because image object outputs an array and not just the URL. Jun 7, 2013 at 8:02
0

I have been trying to figure this out forever! Perfect solution, Ashraf.

Just to clarify for other newbies like me...Go to the Advanced Custom Fields dashboard & edit the field where it says 'Return Value'. Select the radio button for 'Image URL' instead of object or ID. It works perfectly in Genesis. Here is the code I'm using for my image field 'rating_award_2':

add_action('genesis_sidebar', 'rating_award_2');

function rating_award_2() {
if(is_single() ) 
if( get_field('rating_award_2') ):
?><img src="<?php the_field('rating_award_2'); ?>" alt="" /><?php
endif;
}

Your Answer

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

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