Support

Account

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

Solved

Implode Post Object with "," and add "and" at last word

  • I have post object listing developments, for example;

    • Development A
    • Development B
    • Development C

    I would like to it output as;

    Development A, Development B and Development C

    I’ve tried the below, but it just blows up…

    <?php 
    	$development = the_sub_field('development', $post_id)
    	$last  = array_slice($development, -1);
    	$first = join(', ', array_slice($development, 0, -1));
    	$both  = array_filter(array_merge(array($first), $last), 'strlen');
    	echo join(' and ', $both);
    ?>

    Any ideas?

  • I don’t see why it wouldn’t work in principle as long as you have at least 2 items in the array. But it will fall apart when there are none or 1. Although I do not understand your purpose for using array_filter() and filtering all the values through strlen() as this would return an array of the string lengths of the elements.

    
    $development = the_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;
    }
    
  • @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(); ?>
  • You are likely getting empty results, you should probably wrap the if else in another check

    
    $output = false;
    if (!empty($development)) {
      if (count($development ....
    }
    
  • 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…

  • You’ll need to do something else

    
    $location_count = count($locations);
    $counter = 0;
    foreach ($locations as $location) {
      $counter ++;
      if ($counter > 1) {
       if ($counter < $location_count) {
         echo ', ';
       } else {
         echo ' and ';
       }
      }
      // rest of your code here
    }
    
  • 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…

  • That space is likely caused by the return and intending before your output of the span tag, you need to eliminate any extra white-space between ?> and ‘<?php` tags

    
    ?><span class="accent_color"><?php echo get_the_title( $location->ID ); ?></span><?php
    
  • Yep, that solved it! Thanks for much for the help @hube2

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Implode Post Object with "," and add "and" at last word’ is closed to new replies.