如何在Wordpress中输出自定义字段名称和值
问题描述:
我创建了一个自定义帖子类型,并在其中添加了一些自定义字段。如何在Wordpress中输出自定义字段名称和值
目前我的循环看起来像这样:
<?php
//* The Query
$exec_query = new WP_Query(array (
'post_type' => 'jobs',
'job_role' => 'fryking',
'posts_per_page' => 4,
'order' => 'ASC'
));
//* The Loop
if ($exec_query->have_posts()) {
while ($exec_query->have_posts()): $exec_query->the_post();
echo '<div class="subcategory">';
echo '<h3 class="cat_title">';
the_title();
echo '</h3>';?>
<div class="cat_content">
<div class="left">
<?php the_content();
$url = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
the_field('hake_and_chips');
?>
</div>
<div class="right">
<?php
echo '<div class="menu_image" style="background: url('. $url.')">';
echo '</div>';?>
</div>
</div>
</div>
<?php
endwhile;
//* Restore original Post Data
wp_reset_postdata();
}
?>
我设法让我的字段值与此代码:
the_field('hake_and_chips');
我怎样才能得到字段名?
希望你能帮助
答
这些字段存储在元后表,让你可以使用get_post_meta
功能也是这个自定义字段的值。
试试这个代码来获得自定义字段的单个值:
echo get_post_meta($post->ID, 'hake_and_chips', true);
希望这能对你有帮助。谢谢。
此代码与我目前为止所做的相同,它显示自定义字段的值。自定义字段有一个名称(在这个例子中是Hake和chips)和一个值。 我需要显示的名字以及 –
然后你可以尝试像这样:$ field_object = get_field_object('hake_and_chips',$ post-> ID); echo $ field_object ['label']; –