• Hi everyone, I have this code

    <?php
    						$imagem = new WP_Query(array(
    							'post_type' => 'post',
    							'posts_per_page' => -1, //Se deixar em branco ele pega o default do Admin, Deixe -1 para ilimitado
    							'meta_key' => 'imagem', //Aqui fica o name do seu custom_field (é o que está no select dropdown)
    							'orderby' => 'meta_value', //Deixe assim
    							'order' => 'DESC', //Do maior para o menor, mude para ASC se quiser do menor para o maior.
    							'category_name' => 'outfits'
    						));
    
    						while ($imagem->have_posts()): $imagem->the_post();
    						$link = get_post_meta( $post->ID, 'imagem', true );
    
    						    echo '
    <ul>';
    						    echo '
    <li><a href="#"><img src="'.$link.'" width="621"/></a></li>
    ';
    						    echo '</ul>
    ';
    
    						endwhile
    					?>

    [Please post code & markup between backticks or use the code button. Your posted code has now been permanently damaged by the forum’s parser.]

    I wanna that this code return me a imageof custom field in loop. Just one image appear. I wanna + images. How I do this? Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Aline_rodrigues

    (@aline_rodrigues)

    <?php
    						$imagem = new WP_Query(array(
    							'post_type' => 'post',
    							'posts_per_page' => -1, //Se deixar em branco ele pega o default do Admin, Deixe -1 para ilimitado
    							'meta_key' => 'imagem', //Aqui fica o name do seu custom_field (é o que está no select dropdown)
    							'orderby' => 'meta_value', //Deixe assim
    							'order' => 'DESC', //Do maior para o menor, mude para ASC se quiser do menor para o maior.
    							'category_name' => 'outfits'
    						));
    
    						while ($imagem->have_posts()): $imagem->the_post();
    						$link = get_post_meta( $post->ID, 'imagem', true );
    
    						    echo '
    <ul>';
    						    echo '
    <li><a href="#"><img src="'.$link.'" width="621"/></a></li>
    ';
    						    echo '</ul>
    ';
    
    						endwhile
    					?>

    [Please post code & markup between backticks or use the code button. Your posted code has now been permanently damaged by the forum’s parser.]

    Moderator bcworkz

    (@bcworkz)

    Depends on how you want to specify multiple image URLs. You could simply enter the URLs in your custom field separated by commas. Then after you get_post_meta() you would explode() the field value. Then run a foreach loop on the resulting array. After using trim() on the URL, you can echo out the necessary HTML.

    Thread Starter Aline_rodrigues

    (@aline_rodrigues)

    Hi bcworkz, Thanks by the answer, Would you show me a example, sorry but I don’t have much knowledges about that. ??

    Ps: sorry about my bad english.

    Moderator bcworkz

    (@bcworkz)

    Typical field value:
    https://example.com/wp-content/uploads/2012/08/image1.jpg,https://example.com/wp-content/uploads/2012/08/image2.jpg

    Typical template code, inside the “Loop” (untested):

    $imgs = get_post_meta(get_the_ID(), 'images', true);
    $images = explode(',', $imgs);
    foreach($images as $image) {
       $image = trim($image); //just in case somebody entered whitespace with commas
       echo "<img src=\"$image\"><br>";
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get Custom Field Image Loop’ is closed to new replies.