Support

Account

Home Forums Add-ons Gallery Field Gallery images not displaying on SOME pages

Solved

Gallery images not displaying on SOME pages

    • emmaee

    • July 16, 2013 at 12:57 am

    I have an ACF gallery that won’t display images in a custom post category page, but will show on the archive page. They both use the content-cpt.php template, and all other advanced custom fields display fine.

    My CPT name is Projects

    Archive (I have one block of code for each category):

    <?php /* Kitchens */ 
    	$loop = new WP_Query( array( 'post_type' => 'project', 'category_name' => 'kitchens', 'posts_per_page' => 4) ); 
    	if($loop->have_posts()){ ?>
    		<h2>Kitchens</h2>
    		<?php while ( $loop->have_posts() ) : $loop->the_post(); 
    			get_template_part( 'content', 'project' );
    		endwhile; 
    		wp_reset_postdata();
    	}?>

    Category page:

    <?php if(get_post_type() == 'project') {
    	$cat = get_category( get_query_var( 'cat' ) );
    	$cat_slug = $cat->slug;
    	$loop = new WP_Query( array( 'post_type' => 'project', 'category_name' => $cat_slug) ); 
    	if($loop->have_posts()){ ?>
    		<?php while ( $loop->have_posts() ) : $loop->the_post(); 
    			get_template_part( 'content', 'project' );
    		endwhile;
    		wp_reset_postdata();
    	} 
    }?>

    Content template:

    <div class="entry-content">
    	<?php global $post;
    	<?php $images = get_field('photos', $post->ID );?>
    	
    	<div class="main-photo"> 
    		<img src="<?php echo $images[0]['sizes']['project-thumb']; ?>"/> 
    	</div>
    	<?php the_field('project_location', $post->ID ); ?>
    	<?php echo $post->ID; the_content(); ?>
    	<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'ab_k' ), 'after' => '</div>' ) ); ?>
    
    </div><!-- .entry-content -->

    Any help would be appreciated.

    • Elliot

    • July 16, 2013 at 12:45 pm

    Hi @emmaee

    I’m not sure of the exact issue, but I would start by debugging your variables.
    Have you printed the value of $post->ID to test that it is correct?

    • emmaee

    • July 17, 2013 at 12:59 am

    Yes, I have tested that as well, and it is correct. I also used this code to display the values of all the fields:

    $fields = get_fields( $post->ID );?>
    		<pre> <?php var_dump($fields) ?> </pre>

    I then compared the output on both the archive page and category pages. Everything is the same except on the archive page, the “photos” field shows an array filled with different photos and sizes, whereas the output on the category pages has a completely empty array.

    ["photos"]=>
      array(0) {
      }
    • Elliot

    • July 17, 2013 at 2:35 pm

    Hi @emmaee

    Have you printed the value of $post->ID to test that it is correct (not the get_field value, just the $post->ID)?

    • emmaee

    • July 17, 2013 at 11:34 pm

    Yes, I used
    echo $post->ID;
    and it prints the correct ID for the post.

    • emmaee

    • July 18, 2013 at 12:27 am

    I don’t know if this will help, but I printed the values using two methods: get_fields and get_post_custom.

    get_fields:

    <?php $fields = get_fields( $post->ID );?>
    <pre> <?php var_dump($fields) ?> </pre>

    Printed:

    ["photos"]=>
    array(0) {
    }

    get_post_custom:

    <?php $images = get_field(‘photos’, $post->ID );?>
    <pre><?php $c = get_post_custom(); var_dump($c); ?></pre>

    Printed:

    ["photos"]=>
    array(1) {
    [0]=>
    string(62) “a:4:{i:0;s:3:”123″;i:1;s:3:”125″;i:2;s:3:”124″;i:3;s:3:”126″;}”
    }
    ["_photos"]=>
    array(1) {
    [0]=>
    string(19) “field_51dad81f8f6b2″
    }
    • Elliot

    • July 18, 2013 at 1:04 pm

    Have you debugged the get_field functiuon?

    
    $images = get_field('photos', $post->ID );
    
    <pre><?php var_dump($images); ?></pre>
    
    • emmaee

    • July 19, 2013 at 12:18 am

    I believe so. That outputs an empty array. The name ‘photos’ must be correct since they display fine on the archive page.

    If I replace the ‘photos’ with any other custom field name for that post, that displays fine.

    If I don’t use the $post->ID parameter, the photos still display fine on the archive page, but not for the category pages.

    I tried moving the code from the content-project.php template to both the archive and category pages, still no changes.

    Why would two pages, using the same code, display one custom field differently?

    • Elliot

    • July 20, 2013 at 12:09 pm

    Hi @emmaee

    Perhaps I am miss-interpreting your terminoligy.

    When you say ‘category pages’, what do you mean. Do you mean a post’s archive page based on a category?

    • emmaee

    • July 20, 2013 at 12:56 pm

    Yes, I mean category.php is using code from content-project.php to display a preview of a custom post type. archive-project.php is also using this same code from content-project.php to display the same preview on an archive page. They are both pulling the template code via get_template_part('content', 'project');. Every field for a project cpt can be displayed except the gallery field when viewing a category. That is the only field that doesn’t work, and only when viewing a category.

    • Elliot

    • July 22, 2013 at 12:25 pm

    Hi @emmaee

    Just to confirm, are you saying that all get_field / the_field calls are returning the correct data in both view, but the gallery field data is not being returned in one of the views?

    I wonder if your theme is using a custom filter on the post meta which is causing ACF to fail loading the value. Potentially it is only for the custom field name of ‘photos’.

    Perhaps you could create another gallery field with a different name and try that.

    • emmaee

    • July 22, 2013 at 11:39 pm

    Elliot,

    Yes, your description of the problem is correct.

    Creating a new gallery with a new name did not change anything. I work for a web development company, and we make our own themes so I didn’t think there would be a filter like that, but I tried it anyway. Any other ideas?

    Thanks for your help!

    • Elliot

    • July 23, 2013 at 3:28 pm

    Hi @emmaee

    Can you try loading the gallery data at a WP level. As in, can you use the get_post_meta function (documentation on WP.org)?

    Does that work?

    Maybe there is a bug with the get_field function….

    • emmaee

    • July 24, 2013 at 5:38 am

    Ah ha! That worked!

    <?php global $post;
    	$values = get_post_meta( $post->ID, 'photos');
    	$images = wp_get_attachment_image_src( $values[0][0], 'project-thumb' ); ?>
    
    <div class="main-photo"> 	
    	<img src="<?php echo $images[0]; ?>"/>  
    </div>
Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.