• Resolved yogesvamp

    (@yogesvamp)


    I have defined three custom fields namely and i have them in my page-home.php. It gives me back the image url

    $inside_image_1 = get_field(‘inside_image_1’);
    $inside_image_2 = get_field(‘inside_image_2’);
    $inside_image_3 = get_field(‘inside_image_3’);

    I want to have some something like

    <a class="project-item web-design" href="http://www.ynnt.com" data-images="assets/projects/folder/web.jpg,assets/projects/folder/lh.jpg">

    The custom field is not required. I want to write a query to display image 1 url if user has uploaded one, display image1 and 2 and three if he has uploaded both or all the images like above where user has two images uploaded.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can run conditional statements and if they’re true add them to a previously empty array. Then use PHP’s implode() function to seperate the array by commas. Here we store everything in a $images_array array variable:

    <?php
    	$inside_image_1	= get_field( 'inside_image_1' );
    	$inside_image_2	= get_field( 'inside_image_2' );
    	$images_array 	= array();
    
    	if( ! empty( $inside_image_1 ) ) {
    		$images_array[] = $inside_image_1;
    	}
    
    	if( ! empty( $inside_image_2 ) ) {
    		$images_array[] = $inside_image_2;
    	}
    ?>
    
    	<a href="example.com"<?php echo ( ! empty( $images_array ) ) ? ( 'data-images="' . implode( ',', $images_array ) . '"' ) : ''; ?>>Somekind of Link Text</a>
    Thread Starter yogesvamp

    (@yogesvamp)

    Thank you so much .. can’t wait to try it as soon i as reach home ..
    I really appreciate your help..

    Thread Starter yogesvamp

    (@yogesvamp)

    Ok this works like charm .. thanks a million

    <a class="project-item <?php the_field('data_filter'); ?>"  href="#" 
    
    <?php
    	$inside_image_1	= get_field( 'inside_image_1' );
    	$inside_image_2	= get_field( 'inside_image_2' );
    	$inside_image_3	= get_field( 'inside_image_3' );
    	$images_array 	= array();
    
    	if( ! empty( $inside_image_1 ) ) {
    		$images_array[] = $inside_image_1;
    	}
    
    	if( ! empty( $inside_image_2 ) ) {
    		$images_array[] = $inside_image_2;
    	}
    
    	if( ! empty( $inside_image_3 ) ) {
    		$images_array[] = $inside_image_3;
    	}
    ?>
    
    <?php echo ( ! empty( $images_array ) ) ? ( 'data-images="' . implode( ',', $images_array ) . '"' ) : ''; ?>>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘About custom fields’ is closed to new replies.