Perfect thanks John, I can see now where I was going wrong 🙂
I removed that thanks, but still no change;
<?php
$count = count( get_sub_field('featured_developments') );
echo '<strong>The total is '. $count . '</strong></br><br>';
if( have_rows('featured_developments') == 1 ) {
?>
<?php while( have_rows('featured_developments') ): the_row(); ?>
<?php
$post_id = get_sub_field('development', false, false);
?>
<div class="lg-col-12">
</div>
<?php endwhile; ?>
<?php
} elseif( have_rows('featured_developments') == 2 ) {
?>
<?php while( have_rows('featured_developments') ): the_row(); ?>
<?php
$post_id = get_sub_field('development', false, false);
?>
<div class="lg-col-6 md-col-6">
</div>
<?php endwhile; ?>
<?php
} else{
?>
<?php while( have_rows('featured_developments') ): the_row(); ?>
<?php
$post_id = get_sub_field('development', false, false);
?>
<div class="lg-col-4 md-col-4">
</div>
<?php endwhile; ?>
<?php
}
?>
<?php wp_reset_query(); ?>
Hi John,
Thanks for getting back to me, I tried that and didn’t work the “echo $count = 2” was correct so I used this code;
<?php
$count = count( get_sub_field('featured_developments') );
echo '<strong>The total is '. $count . '</strong></br><br>';
if( have_rows('featured_developments') == 1 ) {
?>
<?php while( have_rows('featured_developments') ): the_row(); ?>
<?php
$post_id = get_sub_field('development', false, false);
?>
<div class="lg-col-12"></div>
<?php $count++; ?>
<?php endwhile; ?>
<?php
} elseif( have_rows('featured_developments') == 2 ) {
echo '2';
?>
<?php while( have_rows('featured_developments') ): the_row(); ?>
<?php
$post_id = get_sub_field('development', false, false);
?>
<div class="lg-col-6 md-col-6"></div>
<?php $count++; ?>
<?php endwhile; ?>
<?php
} else{
?>
<?php while( have_rows('featured_developments') ): the_row(); ?>
<?php
$post_id = get_sub_field('development', false, false);
?>
<div class="lg-col-4 md-col-4"></div>
<?php $count++; ?>
<?php endwhile; ?>
<?php
}
?>
<?php wp_reset_query(); ?>
It outputted “lg-col-12”, when it should’ve outputted “lg-col-6”
That seems to be working thanks John! 🙂
Hi @hube2
Sorry, I am using the relationship field in 1 CPT to get data from another (hope that makes sense?)
So I have the loop of CPT (1) and inside that done this;
<?php
$dev_id = get_the_ID();
$doctors = get_posts(array(
'post_type' => 'house_types',
'meta_query' => array(
array(
'key' => 'development_plots', // name of custom field
'value' => $dev_id, // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
if( $doctors ):
?>
<?php
foreach( $doctors as $doctor ):
?>
<?php
$field = get_field_object( 'bedrooms', $doctor->ID );
$value = $field['value'];
$label = $field['choices'][ $value ];
?>
<?php echo $value; ?> //these being 5 5 2 etc.
<?php endforeach; ?>
<?php endif; ?>
Where at the end I was hoping to use the PHP from my previous message to show the min/max of the select fields, e.g.
Min: 2
Max: 5
I’ve made some progress on this, I’m looking for this to work with my select field;
<?php
$array = array(3, 5, 5);
echo (min($array)). ' - ' .(max($array)) ;
?>
So I can get the CPT loop of the ACF fields, but when I try and get that to show the min/max of above it errors
Yep, that solved it! Thanks for much for the help @hube2
That’s 99.9% there thanks @hube2 !!! 🙂
The only annoyance is a random space before the first comma;
Kimberley Brewary , Shipstones Brewary and Mansfield Brewary
When it should be
Kimberley Brewary, Shipstones Brewary and Mansfield Brewary
I’ve tried to work out what’s causing it, but can’t seem to crack it…
I’m getting latlng is not defined error and nothing showing…
Thanks for the help @hube2
To make sure I wasn’t getting empty results, I changed the ACF to relationship and used;
<?php $locations = get_field('development_plots'); ?>
<?php if( $locations ): ?>
<?php foreach( $locations as $location ): ?>
<span class="accent_color"><?php echo get_the_title( $location->ID ); ?></span>,
<?php endforeach; ?>
<?php endif; ?>
To output them, which they are but obviously want the last one to have ‘and’ at the end.
I tried the above, but that just blew it up again…
Thanks for the reply and support, I used array_filter()
etc. from another tutorial which I came across.
I inserted the code you gave and got some errors and realised that it’s needed more code around it. I worked through some but now get;
Warning: count(): Parameter must be an array or an object that implements Countable in C:\laragon\www\joint-venture\wp-content\themes\joint-venture\single-house_types.php on line 15
9
Warning: count(): Parameter must be an array or an object that implements Countable in C:\laragon\www\joint-venture\wp-content\themes\joint-venture\single-house_types.php on line 15
1
With this code;
<?php if( have_rows('development_plot_number') ): ?>
<?php while( have_rows('development_plot_number') ): the_row(); ?>
<?php
$development = get_sub_field('development', $post_id);
$output = false;
if (count($development) == 1) {
$output = $development[0];
} elseif (count($development) > 1) {
$last = array_slice($development, -1);
$first = join(', ', array_slice($development, 0, -1));
$both = array_merge(array($first), $last);
$output = join(' and ', $both);
}
if ($output) {
echo $output;
}
?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
That’s perfect thanks! I had the feeling I wasn’t far off.
Solved it with;
<?php if( $value == '' ): ?>
<?php else: ?>
<div class="product_logo">
<img src="<?php echo $logos[$value] ?>" alt="<?php the_field('product_logo') ?>">
</div>
<?php endif; ?>
I’ve had a similar issue where my query had a lot of arrays;
'meta_query' => array(
'relation' => 'AND',
// array(
// 'key' => 'product_type',
// 'value' => $filterProductType,
// 'compare' => '=',
// ),
// array(
// 'key' => 'load_class',
// 'value' => $filterProductUsed,
// 'compare' => '=',
// ),
// array(
// 'key' => 'length_of_opening',
// 'value' => $lengthValue,
// 'compare' => '=',
// ),
// array(
// 'key' => 'width_of_opening',
// 'value' => $widthValue,
// 'compare' => '=',
// ),
// array(
// 'key' => 'depth',
// 'value' => $depthValue,
// 'compare' => '=',
// ),
array(
'key' => 'lockable',
'value' => $lockAble,
'compare' => 'LIKE',
),
array(
'key' => 'grating_opening_orientation',
'value' => $grateOpening,
'compare' => 'LIKE',
),
array(
'key' => 'cover_type',
'value' => $coverType,
'compare' => 'LIKE',
),
array(
'key' => 'enhanced_skid_resistance',
'value' => $enhancedSkid,
'compare' => 'LIKE',
),
array(
'key' => 'accessories',
'value' => $accessoriesOpt,
'compare' => 'LIKE',
),
array(
'key' => 'sealable',
'value' => $sealableOpt,
'compare' => 'LIKE',
),
array(
'key' => 'install_plus',
'value' => $installPlus,
'compare' => 'LIKE',
),
),
So been trying to do the above, however it doesn’t run the query anymore and comes back with no “products” found.
$results = array_unique ( array_intersect( $default_types, $first_result_types, $lockable_types, $grate_types ) );
$posts = new WP_Query( $results );
if ( $posts->have_posts() ){
while($posts->have_posts()){
$posts->the_post();
get_template_part( 'template-parts/content' );
}
}
else {
print '<h1>Sorry no products matched the filter</h1>';
}
I realised that works on the page with the custom fields associated but not on another…so how do I get it to populate there??
Got it with;
echo the_field('background_image', $post_id);
Cheers
I tried it and got an error;
<b>Fatal error</b>: Uncaught Error: Call to undefined function get_the_field()
Hi @bosoxbill
Thanks for getting back to me, sadly it didn’t work. It put the car at £11,000 at the bottom with a £12,000 and £7,000 above it.
UPDATE
Went back and removed the £ sign from the fields and tested again, worked! Thanks! 🙂
Thanks for all the comments, I solved it by adding the following to my functions.php
add_filter('acf/format_value/type=textarea', 'do_shortcode');
I have been able to get the page title but can’t get the custom field inside of it….
<?php if( have_rows('page_selector') ):
$i=1;
$count = (count($my_fields['value']));
?>
<?php while( have_rows('page_selector') ): the_row();
// vars
$page = get_sub_field('page');
?>
<div class="lg-col-6 md-col-6">
<div class="sub_service">
<?php
// vars
$post_id = get_sub_field('page', false, false);
// check
if( $post_id ): ?>
<a href="<?php echo get_the_permalink($post_id); ?>"><h2><?php echo get_the_title($post_id); ?></h2></a>
<?php endif; ?>
<p>Content</p>
</div>
</div>
<?php
$i++;
endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Solved!
Since it’s in an repeater, it needed to be;
$date = get_sub_field('event_date', false, false);
instead of “get_field” it needed to be “get_sub_field”
For some reason won’t format the code how it should do normally…
As always John you are fantastic!
As always it’s only one little thing wrong!! Grrr
Cheers! 🙂
Brilliant, thanks John 🙂
Hi @acf-support
That worked, thanks! Can’t believe I didn’t realise it needed to be a sub field
Thanks 🙂
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.