代码之家  ›  专栏  ›  技术社区  ›  dmanexe

返回WordPress自定义字段值

  •  2
  • dmanexe  · 技术社区  · 17 年前

    我正在尝试将自定义字段(值,如编辑日志时设置的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,只是没有返回值。

    2 回复  |  直到 17 年前
        1
  •  2
  •   sepehr GR7    17 年前

    请加 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;
    ?>
    
        2
  •  0
  •   Michael    17 年前

    不确定 category=3 工作,但使用 cat=3 在你 get_posts 语句。

    还需要 setup_postdata($post); 在你的foreach陈述之后。

    Sepehr Lajevardi的解决方案也应该很好地工作;)

    推荐文章