Shane Eckert

Team Lead @ Automattic

WordPress: Adding custom fields

WordPress: Adding custom fields

Every day that I get a CrossFit workout in, I like to record that on my site, Level Up CrossFit. But that is a time consuming task that I have less and less time for. I don’t want to stop doing that, so I decided to add some custom fields so that entry is easier and formatting is all done.

Enter Advanced Custom Fields.

This plugin makes it very simple. Just add the custom fields in the Admin and then edit your templates to display.

Here is what my extra fields look like in Advanced Custom Fields.

And this is what that looks like when Adding or Editing a post.

This is the simple PHP I added to content-single.php in the theme I am using.

    <?php echo "<h3>MetCon</h3>" ?>
    <?php if( get_field('metcon') ): ?>
    <p><?php the_field('metcon'); ?></p>
    <?php endif; ?>
    <?php echo "<h3>Strength</h3>" ?>
    <?php if( get_field('front_squats') ): ?>
    <p>Front Squats: <?php the_field('front_squats'); ?></p>
    <?php endif; ?>
    <?php if( get_field('strict_push_press') ): ?>
    <p>Strict Push Press: <?php the_field('strict_push_press'); ?></p>
    <?php endif; ?>
    <?php if( get_field('back_squats') ): ?>
    <p>Back Squats: <?php the_field('back_squats'); ?></p>
    <?php endif; ?>
    <?php if( get_field('dead_lifts') ): ?>
    <p>Dead Lifts: <?php the_field('dead_lifts'); ?></p>
    <?php endif; ?>
    <?php if( get_field('bench_press') ): ?>
    <p>Bench Press: <?php the_field('bench_press'); ?></p>
    <?php endif; ?>
    <?php if( get_field('barbell_rows') ): ?>
    <p>Barbell Rows: <?php the_field('barbell_rows'); ?></p>
    <?php endif; ?>
    <?php echo "<h3>WOD Results</h3>" ?>
    <?php if( get_field('wod_rx') ): ?>
    <p>RX: <?php the_field('wod_rx'); ?></p>
    <?php endif; ?>
    <?php if( get_field('wod_time') ): ?>
    <p>Time: <?php the_field('wod_time'); ?></p>
    <?php endif; ?>

The finished result looks like this.