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.

How do I retrieve a custom post type image URL? All I accomplish is to get the widht and height of the image but not the actually URL.

My code looks like this and are run in the functions.php of my theme:

function fields_in_feed($content) {  
if(is_feed()) {  
    $post_id = get_the_ID();  
    $img = get_post_field('img', $post_id);

    $output = '<acfTitle>' . get_post_meta($post_id, 'titel', true) . '</acfTitle>';  
    $output .= '<acfImage>' . $img . '</acfImage>';  
    $output .= '<acfText>' . get_post_meta($post_id, 'text', true) . '</acfText>';  
}  
    return $output;  
}  
add_filter('the_content','fields_in_feed');

If I add $img[0] or $img[1] it only returns the widht and height. Nothing more is stored in the $img array, no more values.

share|improve this question
    
Are you sure that img is a valid field to pass to get_post_field()? –  Chip Bennett Oct 15 '13 at 13:01
    
Hey, Are you using Advanced Custom Fields (wordpress.org/plugins/advanced-custom-fields)? –  Nikhil Jun 10 at 7:36

2 Answers 2

get_post_field is function to get posts fields which are present in the posts table in the database. To get featured image of the post use get_the_post_thumbnail function.Add the below line in your code

  $output .= '<acfImage>' . get_the_post_thumbnail($post_id, 'thumbnail') . '</acfImage>';  

for more details see below link

http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

share|improve this answer
    
The case is that it is an ACF (Advanced custom field) so no featured image. Any clue how I solve this? –  JohnSmith Oct 15 '13 at 13:29

If you are using Advanced Custom Fields you can use the get_field() method.

function fields_in_feed($content) {  
    if(is_feed()) {  
        $post_id = get_the_ID();  
        $image = get_field('img', $post_id);

        $output  = '<acfTitle>' . $img['title'] . '</acfTitle>';  
        $output .= '<acfImage>' . $img['url'] . '</acfImage>';  
        $output .= '<acfText>'  . $img['caption'] . '</acfText>';  
    }  

    return $output;  
}  
add_filter('the_content','fields_in_feed');

This example is for if your custom field is set to return an image object. The object returns an array of information containing URL, title, caption, alt attribute, etc. See more here

share|improve this answer

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.