Home › Forums › General Issues › Issue displaying fields in homepage slider.
Hello, all!
I’m currently working on adding simple functionality to a client website but I’ve ran into an issue getting a custom field to display.
But whenever I try any of the get_field methods in the documentation, it doesn’t end up displaying the field’s data. Just to make sure it wasn’t ACF that’s the problem, I modified the single.php to display the field in the actual post, and that test worked just fine. So I figure the issue must lie in the PHP file that I’m entering it into, which is shown below. The line of PHP I’m trying to add looks like this:
<a href="<?php the_field('registration_url'); ?>"><img src="http://placehold.it/200x70.png" alt="Register Here"></a>
This is the featured.php file that is called by the home.php. Admittedly it’s been a while since I touched PHP, so any help would be much appreciated. Thank you all very much!
<?php
global $ids;
$ids = array();
$arr = array();
$i=1;
$width = 330;
$height = 220;
$width_small = 72;
$height_small = 72;
$featured_cat = get_option('thecorporation_feat_cat');
$featured_num = get_option('thecorporation_featured_num');
if (get_option('thecorporation_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_catId($featured_cat));
else {
global $pages_number;
if (get_option('thecorporation_feat_pages') <> '') $featured_num = count(get_option('thecorporation_feat_pages'));
else $featured_num = $pages_number;
query_posts(array
('post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => (array) get_option('thecorporation_feat_pages'),
'showposts' => (int) $featured_num
));
};
while (have_posts()) : the_post();
$arr[$i]["title"] = truncate_title(50,false);
$arr[$i]["title_small"] = truncate_title(25,false);
$arr[$i]["fulltitle"] = truncate_title(250,false);
$arr[$i]["excerpt"] = truncate_post(470,false);
$arr[$i]["excerpt_small"] = truncate_post(80,false);
$arr[$i]["tagline"] = get_post_meta($post->ID, 'Tagline', $single = true);
$arr[$i]["permalink"] = get_permalink();
$arr[$i]["thumbnail"] = get_thumbnail($width,$height,'thumb',$arr[$i]["fulltitle"],$arr[$i]["tagline"]);
$arr[$i]["thumb"] = $arr[$i]["thumbnail"]["thumb"];
$arr[$i]["thumbnail_small"] = get_thumbnail($width_small,$height_small,'',$arr[$i]["fulltitle"],$arr[$i]["tagline"]);
$arr[$i]["thumb_small"] = $arr[$i]["thumbnail_small"]["thumb"];
$arr[$i]["use_timthumb"] = $arr[$i]["thumbnail"]["use_timthumb"];
$i++;
$ids[]= $post->ID;
endwhile; wp_reset_query(); ?>
<div id="featured-area">
<div class="container clearfix">
<div id="featured-slider">
<?php for ($i = 1; $i <= $featured_num; $i++) { ?>
<div class="featitem clearfix">
<h2 class="feat-heading"><?php echo esc_html($arr[$i]["title"]); ?></h2>
<p class="tagline"><?php echo($arr[$i]["tagline"]); ?></p>
<div class="excerpt">
<p><?php echo($arr[$i]["excerpt"]); ?></p>
<a href="<?php the_field('registration_url'); ?>"><img src="http://placehold.it/200x70.png" alt="Register Here"></a>
<a href="<?php echo esc_url($arr[$i]["permalink"]); ?>" title="<?php printf(esc_attr__('Permanent Link to %s', 'TheCorporation'), $arr[$i]["fulltitle"]) ?>" class="readmore"><span><?php esc_html_e('read more','TheCorporation'); ?></span></a>
</div> <!-- end .excerpt -->
<a href="<?php echo esc_url($arr[$i]["permalink"]); ?>" title="<?php printf(esc_attr__('Permanent Link to %s', 'TheCorporation'), $arr[$i]["fulltitle"]) ?>">
<?php print_thumbnail($arr[$i]["thumb"], $arr[$i]["use_timthumb"], $arr[$i]["fulltitle"] , $width, $height, 'thumb'); ?>
</a>
</div> <!-- end .featitem -->
<?php }; ?>
</div> <!-- div #featured-slider -->
<a id="prevlink" href="#"><?php esc_html_e('Previous','TheCorporation'); ?></a>
<a id="nextlink" href="#"><?php esc_html_e('Next','TheCorporation'); ?></a>
</div> <!-- end .container -->
</div> <!-- end #featured-area -->
<div id="featured-thumbs">
<div class="container clearfix">
<?php for ($i = 1; $i <= $featured_num; $i++) { ?>
<a href="#">
<?php print_thumbnail($arr[$i]["thumb_small"], $arr[$i]["use_timthumb"], $arr[$i]["fulltitle"] , $width_small, $height_small); ?>
</a>
<div class="thumb_popup">
<p class="heading"><?php echo($arr[$i]["title_small"]); ?></p>
<p>"<?php echo($arr[$i]["excerpt_small"]); ?></p>
</div> <!-- end .thumb_popup -->
<?php }; ?>
<div id="active_item"></div>
</div> <!-- end .container -->
</div> <!-- end #featured-thumbs -->
The issue is that when you use the the_field function, ACF does not know which post to get it from! You need to specify the post_id so that ACF can correctly load the value.
You have a while loop which correctly uses the the_post
function, so within this loop, you CAN use the get_field function as per normal to store data into the array like so:
$arr[$i]["custom_field"] = get_field('custom_field');
Then just echo this out in the later loop, like you are doing for all other data.
Thanks
E
That worked like a charm! Thank you very much, Elliot! ACF is working like a champ. Fantastic plugin. Thank you so much for making it available!
The topic ‘Issue displaying fields in homepage slider.’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.