Support

Account

Home Forums Front-end Issues get the feature img from post objects

Solved

get the feature img from post objects

  • Hi guys,
    Saying that I register a post type: product. With ACF, a custom field of post object is set for “product”. I want to pick three releated products by custom field, and display them in the taxonomy loop, HOW TO DO?

    I will use jQuery to show them When hover.
    Sample at Nike http://store.nike.com/at/de_de/pw/neu-herren/meZ7pu

    Tks.

  • Hi @Chris

    Just to clarify, the related products (1, 2 and 3) will be saved to another product, not the taxonomy.

    The taxonomy loop you speak of is to loop through products right?

    Within the products loop, you can load the related products like so:

    
    $related_1 = get_field('related_1');
    

    For performance reasons, I would set the return value setting to ‘Post ID’ for each related product field.

    Now that you have the related product ID (get_field example above), you can load the related product image and link like so:

    
    <?php 
    
    if( $related_1 )
    {
    	echo '<a href="' . get_permalink( $related_1 ) . '">';
    		 echo get_the_post_thumbnail( $related_1, 'thumbnail' );
    	echo '</a>';
    
    }
    
    ?>
    

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

    Hope that helps.

    Thanks
    E

  • Many tks, @elliot.
    Still got a problem. As the code demo above, the link target right, but the thumbnail shown nothing. I’m sure the target post thumbnail was set.
    There are TWO custome field set, checkbox and post object, they will work well?
    I custom a post type “product”, and set a taxonomy “product_category” for it, the code is placed in the loop of taxonomy-product_category.php. My code following:

    <?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<article>
    			<h1></h1>
    			<?php
    				the_content();
    
    				// FIRST CUSTOM FIELD: CHECKBOX FIELD
    				$values = get_field( 'feature' );
    				if ($values) {
    					foreach( $values as $value ) {
    						echo '<span class="label label-feature">' . $value . '</span>'; //WORKING WELL
    					}
    				}
    				
    				// SECOND FIELD: POST OBJECT
    				$related_1 = get_field( '1_related_product' );
    					if( $related_1 ) {
    						echo '<a href="' . get_permalink( $related_1 ) . '">'; // WORKING WELL
    							echo get_the_post_thumbnail( $related_1 ); // RETURN NOTHING
    						echo '</a>';
    					}
    				?>
    				
    				...
  • hi @Chris

    use the full parameters for your get_field, means:
    $related_1 = get_field( '1_related_product', false, false );
    this make ACF get the values from this post.

    and as @Elliot said, use:
    echo get_the_post_thumbnail( $related_1, 'thumbnail' ); to echo the image size you want.

    let me know anything happened.
    A

  • Hi @astrixoblix, it works.
    Thank u and @elliot. And I wanna go farther. As the image shown in my first post, I want to show 3 related produces. Now I repeat the code 3 times.

    $related_1 = 
    ...
    $related_2 = 
    ...
    $related_3 = 
    ...

    is it stupid? Any better idea?

    Here are the codes generated by ACF:

    if(function_exists("register_field_group"))
    {
    	register_field_group(array (
    		'id' => 'acf_releated-products',
    		'title' => 'Releated Products',
    		'fields' => array (
    			array (
    				'key' => 'field_5296d411901eb',
    				'label' => '1st related product',
    				'name' => '1_related_product',
    				'type' => 'post_object',
    				'post_type' => array (
    					0 => 'product',
    				),
    				'taxonomy' => array (
    					0 => 'product_category:213',
    				),
    				'allow_null' => 1,
    				'multiple' => 0,
    			),
    			array (
    				'key' => 'field_5296d53cf9d3e',
    				'label' => '2nd related product',
    				'name' => '2_related_product',
    				'type' => 'post_object',
    				'post_type' => array (
    					0 => 'product',
    				),
    				'taxonomy' => array (
    					0 => 'product_category:213',
    				),
    				'allow_null' => 1,
    				'multiple' => 0,
    			)
    ...
  • @elliot It’s working! Thanks

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

The topic ‘get the feature img from post objects’ is closed to new replies.