• I’m sorry, I cannot think of a way to word this in a search to find what I want, and I am fairly remedial at php so this might be super basic knowledge. Basically, I’m looking to use the following to pull up an unordered list of results:

    <?php if(get_field('prevention')): ?>
    <ul>
    <?php while(has_sub_field('prevention')): ?>
    <li><?php the_sub_field('prevention_method'); ?></li>
    	<?php endwhile; ?>
    </ul>
    <?php endif; ?>

    But I need to alter it so that if the sub_field(‘prevention’) only has 1 result, to just display:

    <?php the_sub_field('prevention_method'); ?>

    with no unordered list around it. There will always be at least 1 result because it is a required field. Is there a way to make this happen? I did not write this code, it was given to me this way so I would prefer to keep it as close to the intended format as possible!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Maybe something like this?

    <?php
    if(get_field('prevention')){
        $prevention_array = array();
        while(has_sub_field('prevention')){
            $prevention_array[] = the_sub_field('prevention_method');
            }
        $i = count($prevention_array);
        if($i==1){
            the_sub_field('prevention_method');
            }
        else{
            echo '<ul>';
            foreach ($prevention_array as $prevention_method) {
                echo '<li>' . $prevention_method . '</li>';
                }
            echo '</ul>';
            }
        }
    ?>

    I haven’t tested this, since I don’t have fields and subfields labelled ‘prevention’ and ‘prevention_method’ but it should work, I think.

    Thread Starter Lunatrick

    (@lunatrick)

    Thank you, thank you, thank you!! That worked!

    Thread Starter Lunatrick

    (@lunatrick)

    Whoops I lied, this is not resolved. It is making the text from the ‘prevention_method’ field appear ABOVE/BEFORE the unordered list (which is blank). Any idea how to get it inside the list?

    So, just to be clear, is the output of my code something like this?

    Prevention Method 1
    
    <ul></ul>
    
    Prevention Method 2
    Prevention Method 3
    Prevention Method 4
    Prevention Method 5

    Try this.

    <?php
    if(get_field('prevention')){
        $prevention_array = array();
        while(has_sub_field('prevention')){
            $prevention_array[] = get_sub_field('prevention_method');
            }
        $i = count($prevention_array);
        if($i==1){
            the_sub_field('prevention_method');
            }
        else{
            echo '<ul>';
            foreach ($prevention_array as $prevention_method) {
                echo '<li>' . $prevention_method . '</li>';
                }
            echo '</ul>';
            }
        }
    ?>

    I think the problem was that I was trying to create an array with the_sub_field() when I should have used get_sub_field(), so that it didn’t immediately echo as output. Let me know if that works.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to determine the number of entries in a field’ is closed to new replies.