Support

Account

Home Forums Front-end Issues Implode Post Object with "," and add "and" at last word Reply To: Implode Post Object with "," and add "and" at last word

  • @hube2

    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(); ?>