我在这里使用这段代码从帖子中获取所有自定义字段,以便在库数组中使用。
http://www.kevinleary.net/get-all-custom-fields-attached-post-page-post-type/
function:
// Get all custom fields attached to a page
if ( !function_exists(\'base_get_all_custom_fields\') ) {
function base_get_all_custom_fields()
{
global $post;
$custom_fields = get_post_custom($post->ID);
$hidden_field = \'_\';
foreach( $custom_fields as $key => $value ){
if( !empty($value) ) {
$pos = strpos($key, $hidden_field);
if( $pos !== false && $pos == 0 ) {
unset($custom_fields[$key]);
}
}
}
return $custom_fields;
}
}
single.php:
// Get all custom fields attached to this post and store them in an array
$custom_fields = base_get_all_custom_fields();
if( !empty($custom_fields) ) {
print_r($custom_fields);
}
它可以工作,但在自定义帖子周围输出如下文本
Array ( [photo1] => Array ( [0] =>
有没有办法定制您之前包装的内容&;在自定义字段输出后,是否删除数组文本?
我想要实现的是
{image : \'http://website.com/slides/photo1.jpg\'},
{image : \'http://website.com/slides/photo2.jpg\'},
{image : \'http://website.com/slides/photo3.jpg\'},
所以我需要打包
{image : \'
之前和
\'},
链接之后。
我可以用单个查询进行排序,但数组系统会更好
{image : \'<?php echo $custom_fields[\'photo1\'][0]; ?>\'},
{image : \'<?php echo $custom_fields[\'photo2\'][0]; ?>\'},
{image : \'<?php echo $custom_fields[\'photo3\'][0]; ?>\'},