Custom fields advice needed

I am using acf.
I want to create a table each row has a person’s name, phone number & other data.
so far i got

<?php
					if(get_field('employee_name')){
						echo'<div class="gAndE">';
						echo'<h2>'.get_field('employee_name').'</h2>';
						echo'<p>'.the_field('desk_phone').'</p>';
						echo'</div>';
					}
				?>

but of course i’d change the styling to something like

<?php
					if(get_field('employee_name')){
						echo'<table class="gAndE">';
						echo '<tr>';
						echo'<td>'.get_field('employee_name').'</td>';
						echo'<td>'.the_field('desk_phone').'</td>';
						echo '</tr>';
						echo'</table>';
					}
				?>

also am wandering if i could create a lop for the info.
so i could put several names in the employee_name filed? Seems like it should work?
could i get some advice on how to do this or get pointed to a tutorial on it?
thx
D

It’s a little bit more work, but not by much:

<?php 
if(get_field('employee_name')){
    echo'<table class="gAndE">'; # Move this OUTSIDE the loop, we only need rows

        # Grab the variables
        $name       = get_field('employee_name');
        $deskPhone  = get_field('desk_phone');

        # Loop through each
        # Note the <?=@ symbol is used to hide possible warnings when $name[$i] is empty
        for($i = 0; $i < count($name); $i++): ?>
            <tr>
                <td><?=@ $name[$i] ?></td>
                <td><?=@ $deskPhone[$i] ?></td>
            </tr>
        <?php endforeach;

    echo'</table>';
}

You can use echo statements if you like, but this is a little cleaner.

Thank you! will go & try it out.
Cleaner is always better!
D

Say the tags don’t seem to be closing appropriately. i tried adding an extra php delimiter but it seems incorrect, could you please advise?
For example you have <?php endforeach; do you meant for that to be closed elsewhere?
thx
D

Strange! Try adding:

?>

At the very end of what I posted, so the complete code should be:

<?php 
if(get_field('employee_name')){
    echo'<table class="gAndE">'; # Move this OUTSIDE the loop, we only need rows

        # Grab the variables
        $name       = get_field('employee_name');
        $deskPhone  = get_field('desk_phone');

        # Loop through each
        # Note the <?=@ symbol is used to hide possible warnings when $name[$i] is empty
        for($i = 0; $i < count($name); $i++): ?>
            <tr>
                <td><?=@ $name[$i] ?></td>
                <td><?=@ $deskPhone[$i] ?></td>
            </tr>
        <?php endforeach;

    echo'</table>';
} ?>

no. i had taken that into account and figured you just assumed i’d put the closing tag in. So did.

				<?php 
				if(get_field('employee_name')){
					echo'<table class="gAndE">'; # Move this OUTSIDE the loop, we only need rows

						# Grab the variables
						$name       = get_field('employee_name');
						$deskPhone  = get_field('desk_phone');

						# Loop through each
						# Note the <?=@ symbol is used to hide possible warnings when $name[$i] is empty
						for($i = 0; $i < count($name); $i++): ?>
							<tr>
								<td><?=@ $name[$i] ?></td>
								<td><?=@ $deskPhone[$i] ?></td>
							</tr>
						<?php endforeach;

					echo'</table>';
				} ?>

gets me error

Parse error: syntax error, unexpected ‘endforeach’ (T_ENDFOREACH) in C:\xampp\htdocs\cdi\wp-content\themes\cdi_oregon\employeeList.php on line 37

i really see no reason for it though.
It is inside the loop, everything looks ok.

	<div id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

		<?php if ( have_posts() ) : ?>

			<?php /* Start the Loop */ ?>
			<?php while ( have_posts() ) : the_post(); ?>

				<?php
					get_template_part( 'content', get_post_format() );
				?>

				<?php 
				if(get_field('employee_name')){
					echo'<table class="gAndE">'; # Move this OUTSIDE the loop, we only need rows

						# Grab the variables
						$name       = get_field('employee_name');
						$deskPhone  = get_field('desk_phone');

						# Loop through each
						# Note the <?=@ symbol is used to hide possible warnings when $name[$i] is empty
						for($i = 0; $i < count($name); $i++): ?>
							<tr>
								<td><?=@ $name[$i] ?></td>
								<td><?=@ $deskPhone[$i] ?></td>
							</tr>
						<?php endforeach;

					echo'</table>';
				} ?>
									

			
			<?php endwhile; ?>

			
			<?php cdi_oregon_paging_nav(); ?>

		<?php else : ?>

			<?php get_template_part( 'content', 'none' ); ?>

		<?php endif; ?>

		</main><!-- #main -->

Oops sorry about that, change the “endforeach” to “endfor”

Thank you Oz. that was great. And just about there. It only gives the first letter of the name & first number of the phone however.
Which makes not sense as in the array you are asking for the full field. I don’t see anywhere in the code why it would only do that.
D

Hey Oz any advice please?

Actually while i’d still love to know why it isn’t reading the full acf field I did find this page that was quite helpful

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.