Support

Account

Home Forums General Issues the_image not returning value in media attachments

Solved

the_image not returning value in media attachments

  • I’m just trying to each a value from a text field into the href of this bit of code. I’ve ensured the field is filled out. In the sourcecode, no value is displayed. Just wonderfi i

    	<div class="featured-image">
    		<a href="<?php the_field('featured_image_link'); ?>" title=""><?php the_post_thumbnail(); ?></a>
    		<span class="caption"><?php  get_post( get_post_thumbnail_id() )->post_excerpt;?> </span>
    	</div>
  • Hi @digisavvy

    What template file is this code from? Is the global $post correct? Does ACF know which post to load the image value from?

    Thanks
    E

  • Hey Elliot,
    Thanks for the prompt reply. This is just a function I’m running that pulls a field from the image/attachment meta. So it would be the page.php template, I suppose. This is a genesis child theme, too, if that helps.

    In the function I didn’t declare the global $post variable originally, but I did just now and it didn’t seem to change anything. I tried getting the field using the_field() method and also as a variable and neither seemed to work.

    Here’s teh function in full, don’t mind the ugly code. =)

    add_action( 'genesis_before_loop', 'ds_add_featured_image', 1 );
    function ds_add_featured_image() {
    	global $post;
    	$variable = get_field('featured_image_link');
    	?>
    
    	<div class="featured-image">
    		<a href="<?php get_field( $variable ); ?>" title=""><?php the_post_thumbnail(); ?></a>
    		<span class="caption"><?php  get_post( get_post_thumbnail_id() )->post_excerpt;?> </span>
    	</div>
    
    <?php }
    
  • Hi @digisavvy
    Perhaps you should debug your code. I would start by finding out if the global $post object is correct.

    Try this:

    
    <?php 
    
    global $post;
    
    echo '<pre>';
    	print_r($post);
    echo '</pre>';
    die;
    
     ?>
    

    What do you get?

  • When I enter that code into functions.php (btw, awesome clean way to do that. Always just did a var_dump, so thanks!)

    WP_Post Object
    (
        [ID] => 169
        [post_author] => 1
        [post_date] => 2013-09-30 04:12:52
        [post_date_gmt] => 2013-09-30 04:12:52
        [post_content] => 
        [post_title] => Our Shop
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => our-shop
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2013-10-01 06:29:28
        [post_modified_gmt] => 2013-10-01 06:29:28
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://www.pspfit.dev/?page_id=169
        [menu_order] => 0
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
        [format_content] => 
    )
  • Hi @digisavvy

    hopefully you can see the returned value is not a URL, but a post object.
    Either you have set the return type to ‘post object’, or ACF is unable to correctly format the value based on the field’s settings.

    From this post object, you can get the url via the get_permalink( $value->ID ) function

    Hope that helps.

    Cheers
    E

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘the_image not returning value in media attachments’ is closed to new replies.