[Custom Fields] Howto hide custom fields when they are empty?
-
Hey,
I did create 4 custom fields:
Meters
Time
Pulsavg
GarminConnectLinkThe idea is when I post in the category “lob” then I will use that category as a traing log.
Based on my date: meter and time I did made a script that calculates the km per hour and minutes per km.
Then problem is I dont know howto not show the field when they are empty…
If you see: https://www.liljefred.dk/frederik/lob/9km-i-traedemollen/ it looks great:
* Meters: 9000
* Time: 00:53:44
* Km/hour: 10.05
* Min/km: 05:58 minutes
* Puls average: 172
* Garmin Connect Link: https://www.test.dkBut at https://www.liljefred.dk/familien-liljefred/fam-liljefred-goes-www/ it doesnt look good as I didnt give that post any values as the post is not meant as a training log:
Warning: Division by zero in /home/frold/liljefred.dk/wp-content/themes/mystique/single.php on line 53
Warning: Division by zero in /home/frold/liljefred.dk/wp-content/themes/mystique/single.php on line 57
* Meters:
* Time:
* Km/hour: 0
* Min/km: 00:00 minutes
* Puls average:
* Garmin Connect Link:The code I use in single.php is:
<?php // Running script by frold @ liljefred.dk //Get the running time $hms = get_post_meta($post->ID, "Time", $single = true); //Get the distance $meters = get_post_meta($post->ID, "Meters", $single = true); //Get the Garmin Connect Link $url = get_post_meta($post->ID, "GarminConnectLink", $single = true); //Get pulsavg. $pulsavg = get_post_meta($post->ID, "Pulsavg", $single = true); //Let us convert the total running time into seconds function hms2sec ($hms) { list($h, $m, $s) = explode (":", $hms); $seconds = 0; $seconds += (intval($h) * 3600); $seconds += (intval($m) * 60); $seconds += (intval($s)); return $seconds; } //Call the function $seconds = hms2sec($hms); //Calculate the avg running speed in km/hour $km_per_hour = (($meters/1000) / ($seconds/3600)); $km_per_hour_round = round($km_per_hour, 2); //Calculate number of minutes per km $min_per_km = ($seconds) / ($meters/1000); $minutes = floor($min_per_km/60); $secondsleft = $min_per_km%60; if($minutes<10) $minutes = "0" . $minutes; if($secondsleft<10) $secondsleft = "0" . $secondsleft; //Print it all echo "<ul class='post-meta'>"; echo "<li><span class='post-meta-key'>Meters:</span> $meters</li>"; echo "<li><span class='post-meta-key'>Time:</span> $hms</li>"; echo "<li><span class='post-meta-key'>Km/hour:</span> $km_per_hour_round</li>"; echo "<li><span class='post-meta-key'>Min/km:</span> $minutes:$secondsleft minutes</li>"; echo "<li><span class='post-meta-key'>Puls average:</span> $pulsavg</li>"; echo "<li><span class='post-meta-key'>Garmin Connect Link:</span> <a href='$url' target='_blank'>$url</a></li>"; echo "</ul>"; //EoM ?>
I hope you can help….
- The topic ‘[Custom Fields] Howto hide custom fields when they are empty?’ is closed to new replies.