I think I’ve got it!
<?php
$images = get_field('header_image', 'option');
$rand = array_rand($images, 1);
if( $images ): ?>
<div>
<img src="<?php echo $images[$rand]['url']; ?>" alt="<?php echo $images[$rand]['alt']; ?>" />
</div>
<?php endif; ?>
Just following up on this to see if anybody can help. I’m sure it’s a very simple thing but I can’t seem to find anything other than random images.
Not saying I’ll be able to fix the problem but it may be something obvious.
Okay, I presume you’ve gone through the steps mentions in my previous posts? And then clicked on Custom Fields > Custom Fields – Clicked on the custom field group you want translating and checked the “Make ‘Field Groups’ translatable”
Hi @mahiro. To be honest it’s a little tricky to know without seeing the WP admin panel. If you get in touch with me via daretothink.co.uk I’ll have a little look and see if it’s anything obvious.
Thanks so much for drumming this into me. I’ve finally figured it out. You were correct on both accounts i.e. needed to be inside the loop and also needed to use
$category->term_id
So the final code I have is
<?php
$cat = get_query_var('cat');
$args = array(
'child_of' => $cat,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
$attachment_id = get_field('cat_half_image', 'category_'. $category->term_id .'');
$size = "half-img";
echo '<div class="cat-block g_6">';
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . wp_get_attachment_image( $attachment_id, $size );
echo '<h2>'. $category->name . '</h2>';
echo '</a>';
echo '<p>'. $category->description . '</p>';
echo '</div>'; }
?>
Umm, I think I may have fixed this but not entirely sure how. Will report back later after I’ve cleared my head!
Apologies for the confusion and thanks for helping out on this. I’ve removed the code that’s actually working from the example below and included the dump of what’s getting produced. It is getting the category ID from the ACF custom field.
Custom fields in Use (to get category ID):
site_cat_link = taxonomy field type (Taxonomy Add-on)
cat_id = Text field type (I’ve just manually added the categories I want to pass, just to see if this works).
Code:
<?php if( get_field('site_cats') ): ?>
<?php while( has_sub_field('site_cats') ): ?>
<?php
$variable = get_sub_field('site_cat_link');
var_dump($variable);
$category_link = get_category_link( $variable );
var_dump($category_link);
?>
<p><?php echo the_sub_field('site_cat_link'); ?></p>
<p><?php echo the_sub_field('cat_id'); ?></p>
<a href="<?php echo esc_url( $category_link ); ?>" >Category</a>
<?php endwhile; ?>
<?php endif; ?>
Output when using cat_id (text field):
Category string(3) “249” string(57) “http://www.mysite.com/inspiration-ideas/”
249
249
Category string(3) “346” string(50) “http://www.mysite.com/tips-advice/”
346
346
Output when using site_cat_link (taxonomy field):
Category array(1) { [0]=> string(3) “249” } string(43) “http://www.mysite.com/news/”
249
249
Category array(1) { [0]=> string(3) “346” } string(43) “http://www.mysite.com/news/”
346
346
What I can’t understand is that even though both are outputting the correct category id’s. The taxonomy addon then doesn’t seem to be able to pass that to the link.
Apologies Elliot.
Here’s all of the code that I’m using
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_field('site_cats') ): ?>
<div class="clearfix" id="site-cats">
<?php while( has_sub_field('site_cats') ): ?>
<div class="cat-block g_6 clearfix">
<div class="image">
<?php echo wp_get_attachment_image(get_sub_field('site_cat_image'), 'half-img'); ?>
</div>
<div class="details">
<h2><?php echo the_sub_field('site_cat_title'); ?></h2>
<p><?php echo the_sub_field('site_cat_description'); ?></p>
<p><?php echo the_sub_field('site_cat_link'); ?></p>
<?php
$variable = get_sub_field('site_cat_link');
$category_link = get_category_link( $variable );
?>
<a href="<?php echo esc_url( $category_link ); ?>" title=""><?php echo the_sub_field('site_cat_title'); ?></a>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
The three sub fields are working correctly i.e. title, description and link are all pulling in their fields but it’s when I then attempt to get the category ID from the site_cat_link field to then produce the category link that I’m having issues.
p.s. I use ACF in a lot of my projects now and would like to donate each time I use it – do you have such an option?
Hi Elliot,
Really appreciate you taking the time to help out with this. What I’m slightly confused about is that the code I have above is working correctly with the exception of the custom image field i.e. the title, link and description are all pulling in from the sub category correctly so I’m not sure what else to do?
Forgive me here, my coding knowledge is not amazing.
Alan
Hi Elliot, I think I’ve figured out what was happening.
Although in WPML > Translation Management > Multilingual Content Setup I had checked the radio buttons for each sub field within the Repeater to “Translate” I had not checked anything for the actual Repeater field itself. For that I checked the “Copy from original to translation”.
This appears, at first play, to have resolved the issue. I shall report back in the next few days or so to make sure this is all working.
Hi. What do you mean about checking the option to translate independently?
I’ve just done another test with a clean install of
Wordpress 2.6
Advanced Custom Fields 4.2.2
WPML 2.9.1
WPML Translation Management 1.7.1
And the problem still exists. Basically the repeater field does not contain any rows and therefore content.
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.