我创建了一个函数和挂钩来在我的一些帖子中插入一些自定义字段信息。
<?php the_meta(); ?>
我添加了一些 CSS 格式(带背景的框)用于显示。问题:如果我的帖子没有定义任何自定义字段,它会显示一个空框。如果没有任何内容可显示,如何防止它输出空?我所能找到的只是有关特定字段类型的信息,并且无法从中推断。我绝对不是 php 老大。
您可以使用 get_post_custom() 将自定义字段作为数组获取,然后仅当数组中存在任何自定义字段时才进行输出。
这应该可以完成工作,尽管它不是很优雅:
$has_custom = false;
foreach(get_post_custom_keys() as $k => $v) {
$t = trim($v);
if('_' != $t{0}) {
$has_custom = true;
break;
}
}
if($has_custom) {
the_meta();
}