Wordpress 从post获取自定义字段值的步骤
我已经得到了后缩略图和后在一个页面的内容由下面的代码Wordpress 从post获取自定义字段值的步骤,wordpress,custom-fields,Wordpress,Custom Fields,我已经得到了后缩略图和后在一个页面的内容由下面的代码 <?php $post_types = array('a', 'b','p','d','f');//post type names foreach( $post_types as $post_type) { // The Query $the_query = new WP_Query( array(
<?php
$post_types = array('a', 'b','p','d','f');//post type names
foreach( $post_types as $post_type) {
// The Query
$the_query = new WP_Query( array(
'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC'
));
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php endwhile; ?>
<?php }?>
现在我想从相应的帖子中获取自定义字段值。用于检索单个键值或整个键值/值对列表(作为数组)
除非$single
参数为true
,否则此函数始终返回数组(即使指定了$key
且数组仅包含一个值)
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(); ?>
<?php $my_key = get_post_meta($post->id, 'my_key', true); ?>
<?php if(!empty($my_key)): ?>
<?php echo $my_key; ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
使用此选项(在循环中):
我有一个自定义字段名“post_img”,我想得到每个单独post的值,如果pot有值,则分配返回值并检查是否设置了值<代码>$value=get_post_meta(…);如果(!empty($value)){/*dostuff*/}
echo get_post_meta(get_the_ID(), 'post_img', true);