<?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 />";
}
?>
实际上,我创建了一个“自定义帖子类型”,并为该帖子类型添加了自定义字段,现在我想在特定帖子类型帖子中显示所有自定义字段值。以上代码显示所有自定义字段。请帮助我仅检索特定帖子的自定义字段。提前感谢。。如何动态显示所有帖子自定义域?
2 个回复
最合适的回答,由SO网友:Praveen 整理而成
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8 left_column"> <?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1> <?php the_title();?> </h1> <?php
$post_meta = get_post_meta(get_the_ID());
foreach($post_meta as $key=>$value)
{
echo "<strong>".$key."</strong>"." => ";
foreach($value as $nameAr=>$valueAr)
{
echo "<br /> ";
echo $nameAr." => ".$valueAr;
}
echo "<br >";
}
the_content();
endwhile;
endif; ?>
</div>
SO网友:Cesar Henrique Damascena
如果你在single_{$post_type_slug}
样板您可以这样做:
// Create an array with the name of all custom field
$custom_field_names = array( \'custom_field1\', \'custom_field2\' );
$custom_fields;
$args = array (
\'post_type\' => \'post\',
\'posts_per_page\' => -1
);
$posts = get_posts( $args );
// Get all the custom fields for this post
foreach( $posts as $key => $post ) {
foreach( $custom_fields_names as $name ) {
$custom_fields[$key][$name] = get_field( $name, $post->ID )
}
}
结束