Support

Account

Home Forums Front-end Issues Custom (image) field empty after WP_Query

Solved

Custom (image) field empty after WP_Query

  • Hi,

    Awesome plugin! I’m using it a lot.

    At the moment I’m running into the following problem:

    I’m having a custom page, everything is inside the have_posts while loop:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php endwhile; endif; ?>
    

    As explained on your website, I use the following code to display an image from an custom field:

    <?php 
    
    $image = get_field('image');
    
    if( !empty($image) ): ?>
    
    	<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    
    <?php endif; ?>

    This works, BUT it stops working right after I use WP_query to display a few custom posts above the image:

     <?php
            $args = array( 'post_type' => 'bruiloft', 'posts_per_page' => 4, 'orderby'   => 'menu_order', 'order'     => 'ASC');
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
            $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );       
        ?>
    <?php endwhile;	?>

    There are custom fields inside the custom post type as well and these work.

    I don’t know what else to do, I need both the custom fields aswell as the custom post type

  • Found the answer on Stack Exchange.

    In order for this to work you need to reset each query with

    wp_reset_query()

    http://stackoverflow.com/questions/23633231/wordpress-page-template-custom-fields-not-working-after-wp-query-loop/26449631

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

You must be logged in to reply to this topic.