最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Separate [Advanced Custom Field] values by commas

programmeradmin12浏览0评论

I have a 'relationship' custom field that allows you to select multiple values and display them via

<?php $post_objects = get_field('field');
   if($post_objects!=''){ ?>
   <?php foreach( $post_objects as $post): ?>
   <?php setup_postdata($post); ?>
       <a href="<?php the_permalink(); ?>">
           <?php the_title(); ?>, 
       </a>
   <?php endforeach; ?>
<?php wp_reset_postdata(); ?>

This works for now but is not very pretty as it returns the values as "Value, Value, Value,". What I would like it to output is...

1 Value = "Value"

2 Values = "Value & Value"

3 or more Values = "Value, Value & Value"

"

I have a 'relationship' custom field that allows you to select multiple values and display them via

<?php $post_objects = get_field('field');
   if($post_objects!=''){ ?>
   <?php foreach( $post_objects as $post): ?>
   <?php setup_postdata($post); ?>
       <a href="<?php the_permalink(); ?>">
           <?php the_title(); ?>, 
       </a>
   <?php endforeach; ?>
<?php wp_reset_postdata(); ?>

This works for now but is not very pretty as it returns the values as "Value, Value, Value,". What I would like it to output is...

1 Value = "Value"

2 Values = "Value & Value"

3 or more Values = "Value, Value & Value"

"

Share Improve this question asked Dec 21, 2013 at 1:48 SBMSBM 721 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Try to grab the output and echo together.

<?php $post_objects = get_field('field');
   if($post_objects!='') :
   $value = array(); ?>
   <?php foreach( $post_objects as $post): ?>
       <?php setup_postdata($post); ?>
       <?php $values[] = '<a href="'. get_permalink() .'">'. the_title('','',false) .'</a>'; ?>
   <?php endforeach; ?>
   <?php endif; ?>
<?php wp_reset_postdata(); ?>

<?php echo join( ', ', $values); ?>

You can use the number_format function of PHP.

You will get something like 2,500

<div>
  <?php if (get_field('events_price_yen')) { ?>
     <p>
        <?php $english_format_number = number_format(get_field('events_price_yen'));
              echo $english_format_number; ?>
     </p>
  <?php }; ?>
</div>
发布评论

评论列表(0)

  1. 暂无评论