Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active October 2, 2022 19:13
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 xnau/1f7526c69d49d5661d38907347ebdf59 to your computer and use it in GitHub Desktop.
Save xnau/1f7526c69d49d5661d38907347ebdf59 to your computer and use it in GitHub Desktop.
Single record template for Participants Database that demonstrates how to skip showing a field based on another value in the record.
<?php
/**
* @name pdb single template
* @version 2.2
*
* demonstrates how to skip displaying a field based on a value in the record
*
* you will need to change 3 values:
* 'conditional_field' should be the name of the field you may want to skip showing
* 'check_field' should be the name of the record value you are checking
* 'check_value' is the value you are looking for to determine whether to show the 'conditional_field' or not
*
*/
if ( $this->record_found() ) :
?>
<div class="wrap <?php echo $this->wrap_class ?>">
<?php while ( $this->have_groups() ) : $this->the_group(); ?>
<div class="section <?php $this->group->print_class() ?>" id="<?php echo Participants_Db::$prefix.$this->group->name ?>">
<?php $this->group->print_title( '<h2 class="pdb-group-title">', '</h2>' ) ?>
<?php $this->group->print_description() ?>
<?php while ( $this->have_fields() ) : $this->the_field();
// check if the field is one we may want to skip
if ( $this->field->name === 'conditional_field' ) {
// now check the record value to determine if it should be skipped
if ( $this->participant_values['check_field'] === 'check_value' ) {
// skip displaying this field
continue;
}
}
// CSS class for empty fields
$empty_class = $this->get_empty_class( $this->field );
?>
<dl class="<?php echo Participants_Db::$prefix.$this->field->name.' '.$empty_class . ' ' . $this->field->element_class() ?>">
<dt class="<?php echo $this->field->name.' '.$empty_class?>"><?php $this->field->print_label() ?></dt>
<dd class="<?php echo $this->field->name.' '.$empty_class?>"><?php $this->field->print_value() ?></dd>
</dl>
<?php endwhile; // end of the fields loop ?>
</div>
<?php endwhile; // end of the groups loop ?>
</div>
<?php else : // content to show if no record is found ?>
<?php $error_message = Participants_Db::plugin_setting( 'no_record_error_message', '' );
if ( ! empty( $error_message ) ) : ?>
<p class="alert alert-error"><?php echo $error_message ?></p>
<?php endif ?>
<?php endif ?>
@xnau
Copy link
Author

xnau commented Oct 2, 2022

Read this article if you are not familiar with using Participants Database custom shortcode templates:

Using Participants Database Custom Templates

You will need to adapt this code to your specific needs. This can also work on multiple fields, you just need to add all the fields you want to show/hide to the conditional on line 31.

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