Skip to content

Instantly share code, notes, and snippets.

@kirstenkeister
Created February 23, 2012 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirstenkeister/1895369 to your computer and use it in GitHub Desktop.
Save kirstenkeister/1895369 to your computer and use it in GitHub Desktop.
Repeating fields for project pages
<?php
global $project_mb;
$project_mb->the_meta();
$i = 0;
while($project_mb->have_fields('links') || $project_mb->have_fields('paststaff') ) {
if($i == 0)
{ ?>
<div id="info-staff" class="column left">
<h2 class="column-title">Participating MITH Staff</h2>
<ul>
<?php } // endif
$staffname = $project_mb->get_the_value('projectstaff');
$paststaffname = $project_mb->get_the_value('past-projectstaff'); ?>
<li><a href="<?php echo get_permalink($staffname); ?>" class="c-staff-link"><?php echo get_the_title($staffname); ?></a></li>
<li><a href="<?php echo get_permalink($paststaffname); ?>" class="p-staff-link"><?php echo get_the_title($paststaffname); ?></a></li>
<?php $i++; } // endwhile
if ($i > 0 ) { ?>
</ul>
</div>
<?php } // endif ?>
<!-- /project-staff -->
@jdickie
Copy link

jdickie commented Feb 28, 2012

This isn't a solution, just a best practices suggestion. In Wordpress, you want to avoid using the 'echo' function to add HTML. Instead, close out your PHP and then use plain HTML. If you need to insert variables, do so by opening and closing the PHP inside of the tags at the proper areas. As an example, this is how I'd write lines 5 - 9 of project-staff:

have_fields('links') ) { // loop through current staff $staffname = $project_mb->get_the_value('projectstaff'); ?>
   <li><a href="<?php get_permalink($staffname) ?> "> <?php get_the_title($staffname) ?>

It looks crazy but it works and goes more with what you normally see in Wordpress. Sometimes, wordpress even omits HTML that you write in echo statements.

@kirstenkeister
Copy link
Author

Yeah, I generally try to do the open/close thing unless it's misbehaving. Sometimes I've had the information not show up properly when I do it with regular HTML, but it's probably fixable without echoing if I kept at it. Regardless, this is a good call and I will try to make open/close php tags standard unless absolutely necessary.

@jdickie
Copy link

jdickie commented Feb 29, 2012

Did you already use the the_field() command to switch fields inside of a while loop? Looking at the WP_Alchemy website, it seems that they want you to set it up so that you pull the data once (do the while $project->have_fields() one time) then switch between data fields by either the_field or the_value calls.

If that isn't working, what I would suggest is that we take a look at your data model and see if it's not satisfying what you're asking it. Sometimes the issue with my stuff is that I design the data for one purpose, then find that I query for something totally different and have to go back and re-design the database. It's painful, but pays off in the long run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment