Home › Forums › Add-ons › Gallery Field › Bring gallery image into have post array
I trying to bring the first gallery image into a protfolio
<?php
$args = array(
'post_type' => 'projects',
'order' => 'ASC'
);
$the_apppage = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_apppage->have_posts() ) : $the_apppage->the_post(); ?>
<?php
$images = get_field('gallery', false, false);
?>
<!-- Image here -->
<img src="<?php echo $images[0]["url"]["large"]; ?>" >
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<small><?php the_excerpt(); ?></small>
<?php endwhile; else: ?>
<p>There are no posts or pages here</p>
<?php endif; ?>
Hi @BeeBlu
The issue is that you are passing the 2 false
parameters to the get_field
function. This will stop ACF from formatting the value and returning data such as ‘url’ or ‘large’.
Perhaps you should change your code to:
$images = get_field('gallery');
Thanks
E
Thanks Elliot but could get that to work. Doing a similar thing today with in a custom post type and having the same issue. I’m pulling the info into a template page, will this make a difference?
Hi @BeeBlu
I don’t know what you mean by Iām pulling the info into a template page, will this make a difference?
Perhaps you could very clearly explain the issue and post your code.
Please also attempt to debug the problem by using a print_r statement to output returned data form the get_field function.
Thanks
E
Sorry elliot still your work with a novice here… still working on my PHP knowledge. Couldn’t get the print_r function to work for me either š
Thanks again for your help, the plugins you have developed are great and it’s change the way I work with and understand wordpress. Cheers
Here is my code for the page
<?php
/*
Template Name: Project Template
*/
?>
<?php get_template_part('templates/content', 'page'); ?>-->
<?php get_template_part('templates/page', 'header'); ?>
<div class="wrap container" role="document">
<div class="content row">
<?php
$args = array(
'post_type' => 'projects',
'order' => 'ASC'
);
$the_apppage = new WP_Query( $args );
?>
<div class="row">
<?php if ( have_posts() ) : while ( $the_apppage->have_posts() ) : $the_apppage->the_post(); ?>
<div class="col-md-4">
<i class="fa fa-arrow-circle-o-right"></i>
<article class="project-item web-design image">
<a class="imageover" href="<?php the_permalink(); ?>">
<!-- Gallery Test Code Here -->
<?php $images = get_field('gallery'); ?>
<h1><img src="<?php echo $images[0]["url"]["thumbnail"]; ?>" ></h1>
<!-- Gallery Test Code Here end -->
<img class="img-responsive project-thumb" src="<?php the_field('gallery_feature_image');?>" alt="<?php the_title(); ?>"><!--Project thumb -->
<div class="details">
<h3 class="project-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><!--Project Title -->
<small><?php the_excerpt(); ?></small><!--Project Categories -->
</div>
</a>
</article>
</div>
<?php endwhile; else: ?>
<p>There are no posts or pages here</p>
<?php endif; ?>
</div>
</div>
Hi @BeeBlu
You can debug like so:
$images = get_field('gallery');
echo '<pre>';
print_r( $images );
echo '</pre>';
die;
Thanks
E
Ok this is what I get out after add that code
Array
(
[0] => Array
(
[id] => 49
[alt] =>
[title] => exet2
[caption] =>
[description] =>
[mime_type] => image/jpeg
[url] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet2.jpg
[width] => 1280
[height] => 905
[sizes] => Array
(
[thumbnail] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet2-150x150.jpg
[thumbnail-width] => 150
[thumbnail-height] => 150
[medium] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet2-300x212.jpg
[medium-width] => 300
[medium-height] => 212
[large] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet2-1024x724.jpg
[large-width] => 1024
[large-height] => 724
)
)
[1] => Array
(
[id] => 50
[alt] =>
[title] => exet1
[caption] =>
[description] =>
[mime_type] => image/jpeg
[url] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet1.jpg
[width] => 1280
[height] => 905
[sizes] => Array
(
[thumbnail] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet1-150x150.jpg
[thumbnail-width] => 150
[thumbnail-height] => 150
[medium] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet1-300x212.jpg
[medium-width] => 300
[medium-height] => 212
[large] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet1-1024x724.jpg
[large-width] => 1024
[large-height] => 724
)
)
[2] => Array
(
[id] => 51
[alt] =>
[title] => exet3
[caption] =>
[description] =>
[mime_type] => image/jpeg
[url] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet3.jpg
[width] => 1280
[height] => 882
[sizes] => Array
(
[thumbnail] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet3-150x150.jpg
[thumbnail-width] => 150
[thumbnail-height] => 150
[medium] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet3-300x206.jpg
[medium-width] => 300
[medium-height] => 206
[large] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet3-1024x705.jpg
[large-width] => 1024
[large-height] => 705
)
)
[3] => Array
(
[id] => 52
[alt] =>
[title] => exet5
[caption] =>
[description] =>
[mime_type] => image/jpeg
[url] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet5.jpg
[width] => 1280
[height] => 777
[sizes] => Array
(
[thumbnail] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet5-150x150.jpg
[thumbnail-width] => 150
[thumbnail-height] => 150
[medium] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet5-300x182.jpg
[medium-width] => 300
[medium-height] => 182
[large] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet5-1024x622.jpg
[large-width] => 1024
[large-height] => 622
)
)
[4] => Array
(
[id] => 54
[alt] =>
[title] => exet4
[caption] =>
[description] =>
[mime_type] => image/jpeg
[url] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet4.jpg
[width] => 1280
[height] => 914
[sizes] => Array
(
[thumbnail] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet4-150x150.jpg
[thumbnail-width] => 150
[thumbnail-height] => 150
[medium] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet4-300x214.jpg
[medium-width] => 300
[medium-height] => 214
[large] => http://169.254.168.252:8888/oxf/wp-content/uploads/2014/01/exet4-1024x731.jpg
[large-width] => 1024
[large-height] => 731
)
)
)
Hi @BeeBlu
This debug info prooves that the images are being loaded. So you can use this in your code as you previous had:
<?php
$images = get_field('gallery');
?>
<!-- Image here -->
<img src="<?php echo $images[0]["url"]["large"]; ?>" >
Hi @BeeBlu
I just noticed the problem!
Change:
<img src="<?php echo $images[0]["url"]["large"]; ?>" >
To
<img src="<?php echo $images[0]["sizes"]["large"]; ?>" >
Remember to always debug your code, and read the docs.
Thanks
E
The topic ‘Bring gallery image into have post array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.