0

I'm trying to hide certain menu items based on the custom fields on a page. I could use CSS or a script to hide the items, that's not the problem. What I don't know is how I can check the custom fields (if field empty then hide item) in my functions.php.

Any ideas?

0
0

I don't know if you need to hide the element completely, as in "never display it", or if you need to hide it conditionally with JavaScript-- say, to get a toggle effect. Either case is similar and would use get_post_meta().

To conditionally display the data use something like:

$m = get_post_meta(get_the_ID(),'my_meta_key');
if(!empty($m)) {
  echo '<div>your data</div>';
}

If you want something that you can toggle with JavaScript use:

$m = get_post_meta(get_the_ID(),'my_meta_key');
$str = '<div %s>your data</div>';
if(!empty($m)) {
  $str = sprintf($str,'class="'.$m.'"'); 
}
echo $str;
1
  • Thanks for the answer. I was aware of get_post_meta() but the problem was that it didn't return anything. I did some digging and found that I had to hook into a function that loads later. I used "shutdown", which worked fine for me. – Michael Dec 28 '15 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.