Php 返回Wordpress自定义字段值

Php 返回Wordpress自定义字段值,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我试图将自定义字段的键(值,如编辑帖子时设置的URL)回显到文档中。以下是总体代码: <div id="feature" class="clearfix"> <?php $feature_post = get_posts('category=3&numberposts=1'); foreach( $feature

我试图将自定义字段的键(值,如编辑帖子时设置的URL)回显到文档中。以下是总体代码:

        <div id="feature" class="clearfix">

                     <?php  
                        $feature_post = get_posts('category=3&numberposts=1');
                        foreach( $feature_post as $post ) : 
                    ?>
                        <div class="feature_post" style='<?php echo get_post_meta($post->ID, 'feature', true); ?>'>
                            <h2><?php the_title(); ?></h2>
                        </div>
                    <?php 
                        endforeach; 
                    ?>

            </div>

具体来说,这是一行代码:

<?php echo get_post_meta($post->ID, 'feature', true); ?>

没有打印任何东西-有什么想法吗


帖子上的自定义字段已经是“feature”,没有CSS问题或Javascript,只是没有返回值。

请添加
global$post
在调用
get_posts()
函数之前,不要在foreach()循环中使用$post命名,然后看看它是否有效!如果失败,只需使用以下代码:

<?php
    $loop = new WP_Query('cat=3&showposts=1');
    if($loop->have_posts()): 
        while($loop->have_posts()): $loop->the_post();
?>
            <div class="feature_post" style="<?php echo get_post_meta($post->ID, 'feature', true); ?>">
                <h2><?php the_title(); ?></h2>
            </div>
<?php
        endwhile;
    endif;
?>


不确定
category=3
是否有效,但在
get\u posts
语句中使用
cat=3

还需要
setup\u postdata($post)在foreach语句之后


Sepher Lajevardi的解决方案也应该很有效;)

如果您让它只回显$post->ID,那么它将返回任何内容。语法正确。愚蠢的问题,但您希望特性的内容在Stlye部分?你看过那页后面的资料了吗?