Home › Forums › General Issues › 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>
</em>
<?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>
<p class="doc_type"><?php if(get_field('incident_ordinance'))?>
<class="incident_ordinance"><?php echo get_field(incident_ordinance) ?> <br><br>
</class="incident_ordinance"></strong></p>
</div>
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.
<div>
<?php
if (get_field('my_field')) {
?>
<p><?php the_field('my_fielde'); ?></p>
<?php
} // end if get_field('my_field')
?>
</div>
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 -->
<em>
<strong><!-- no closing tag or improperly nested -->
<?php si_e('Additional Incident Information:') ?><br><br>
</em>
<?php
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>
<p class="doc_type"><!-- no closing tag -->
<?php
if(get_field('incident_location')) // where does this if end?
?>
<p class="incident_location">
<?php echo get_field(incident_location) ?> <br><br>
</p>
<p class="doc_type">
<?php
if(get_field('incident_ordinance')) // where does this if end
?>
<class="incident_ordinance">
<?php echo get_field(incident_ordinance) ?> <br><br>
</class="incident_ordinance">
</strong><!-- closing tag is not matched or improperly nested -->
</p>
</div>
This site has some online coding classes that are free and I’ve been told that they are pretty good. https://www.codecademy.com/
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.
Ken
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw
— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.