Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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/

share|improve this question
add comment

1 Answer

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');.

share|improve this answer
    
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 '13 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(); ?>"> –  Ashraf Slamang Jun 6 '13 at 12:44
    
How do i put that code into the custom function above which includes the genesis hook? –  Haymanpl Jun 6 '13 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. –  Ashraf Slamang Jun 7 '13 at 8:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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