Home Forums General Issues Need guidance on code cleanup


Need guidance on code cleanup

  • I am working on a site for a client where they have custom layout of an invoice (invoice.php), I have working code, but want it to be more efficient and only show values if they contain data in ACF fields. Can someone offer some guidance on a solution?
    So basically they want if there is data in the ACF Field named “Incident” Type, then the value would appear, if not selected, then everything else should move up in the invoice.

    Sorry for the ugly code, I am still learning the best way.

    <div class="incident_info">
    <p class="doc_type"><em><strong><?php si_e('Additional Incident Information:') ?><br><br>
    <?php if(get_field('incident_type'))?> 
    <class="incident_type"><?php echo get_field(incident_type) ?><br><br></p>
    <p class="doc_type"><?php if(get_field('incident_location'))?> 
    <class="incident_location"><?php echo get_field(incident_location) ?> <br><br>
    <p class="doc_type"><?php if(get_field('incident_ordinance'))?> 
    <class="incident_ordinance"><?php echo get_field(incident_ordinance) ?> <br><br>
  • This forum isn’t really for general coding questions, it’s for questions about how to use ACF, but I’ll get you on your way.

    The first thing you need to do is start using white space and indenting. Here’s a very simple example.

        if (get_field('my_field')) {
            <p><?php the_field('my_fielde'); ?></p>
        } // end if get_field('my_field')

    With proper indenting and white space errors become apparent. Here is your code when I attempt to apply this to it. Things that are missing or incorrect stand out

    <div class="incident_info">
      <p class="doc_type"><!-- no closing tag or improperly nested -->
          <strong><!-- no closing tag or improperly nested -->
            <?php si_e('Additional Incident Information:') ?><br><br>
          if(get_field('incident_type')) // where does this if end?
              <!-- no such thing as a class element, what is this -->
              <!-- <class="incident_type"> commented out the unknown element 
                  assuming you meant p
              <p class="incident_type">
                <?php echo get_field(incident_type) ?><br><br>
              <p class="doc_type"><!-- no closing tag -->
                  if(get_field('incident_location')) // where does this if end?
                      <p class="incident_location">
                        <?php echo get_field(incident_location) ?> <br><br>
                      <p class="doc_type">
                          if(get_field('incident_ordinance')) // where does this if end
                                <?php echo get_field(incident_ordinance) ?> <br><br>
                              </strong><!-- closing tag is not matched or improperly nested -->

    This site has some online coding classes that are free and I’ve been told that they are pretty good.

  • Hi John,

    Thanks for taking the time to help me out, I really do appreciate it. I am marking this as solved while I review the code you provided me.

    I will check out Code Academy as I am sure anything will help me better understand the formatting you mention in addition some additional PHP knowledge.

    Thanks again.


Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Need guidance on code cleanup’ is closed to new replies.