我一直在尝试在我的页面上显示如下所列的所有自定义字段:
Label1: Value1
Label2: Vaule2
等等。
但我只对标签感兴趣-我有以下代码:
<h3>All Post Meta</h3>
<?php $getPostCustom=get_post_custom(); // Get all the data ?>
<?php
foreach($getPostCustom as $name=>$value) {
echo "<strong>".$name."</strong>"." => ";
foreach($value as $nameAr=>$valueAr) {
echo "<br /> ";
echo $nameAr." => ";
echo var_dump($valueAr);
}
echo "<br /><br />";
} ?>
但结果是这样的:
所有Post Meta
_edit_lock =>
0 => string(12) "1462734482:1"
_edit_last =>
0 => string(1) "1"
wpcf-brugernavn =>
0 => string(10) "Kim Domino"
wpcf-afdeling =>
0 => string(6) "Lemvig"
我想展示所有的,除了“
edit_last
“”
edit_lock
“因为这对我的主页没有任何意义。所以我想要这样的列表:
Brugernavn: Kim Domino
Afdeling: Lemvig
SO网友:kaiser
在打印阵列之前,必须遍历/映射/过滤阵列。有很多array_*()
PHP为此提供的函数。示例:
$custom = get_post_custom();
// Remove all strings starting with `_`
array_filter( $custom, function( $data, $name ) {
return "_" !== substr( $name, 0, 1 );
}, ARRAY_FILTER_USE_BOTH );
foreach ( $custom as $field )
var_dump( $field );