Quick question. On my (wordpress-)website, we use a range of custom fields to add additional info to a post. Not all of these fields, in this case the dates (datum2, datum3, datum4, …, datum7) are mandatory, so I only want those that have been filled in to show.
Thing is, it shows ALL the <li>'s, even the ones that have no custom value set for them. For datum7, it shows a list item with only the <br /> tag and the “dag 7” text, while I want no list item at all as “datum7” was left empty.
isset() just tests if a variable is set, which it is in your code above - you’re assigning each variable to the returned value of get_post_custom_values(). Depending on what get_post_custom_values() returns you will need to check for false or a blank string instead.
{get_post_custom_values} returns nothing if no such key exists, or none is entered.
So I don’t know what “nothing” is…false? null? empty string? empty array?
Both functions is_null() and empty() are incorrect. This because empty() will return true for actual values such as; 0,0.00 and the list goes on. That said isset() and is_null() both return false for a null value.
Do a var_dump() on the return values and I will expect WordPress being the pile of trash it is that the method get_post_custom_values() returns an empty string rather then nothing (null).
The wp function in question is supposed to return an array or “nothing”, so empty() will return true for an empty array, empty string, null, false, or 0.
After thinking about this again, there is the possibility that the function literally does not return anything, in which case isset() should work just like you said, but !empty() and !is_null() should also work.
There might be something else going on - I would try a var_dump of each of the variables like oddz suggested, i.e.:
So by empty it means an array with any empty string , what a f**kin pile of sh*t. Since it seems there is always a single item I would just call array_pop() immediately after that get_post_custom_values() function get the actual values your after. Something like the below than use strlen() === 0 to determine the fields without data.
@ThaVincy - You could perhaps create a custom function to handle this - the following should be bullet-proof since that wp function seems to return god-knows-what: