【问题标题】:Returning Wordpress Custom Field Values返回 Wordpress 自定义字段值
【发布时间】:2009-10-21 00:16:23
【问题描述】:

我正在尝试将自定义字段的键(值,例如在编辑帖子时设置的 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); ?>

这不会打印任何东西 - 有什么想法吗?

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

【问题讨论】:

  • 如果你让它只回显 $post->ID 并返回任何东西。语法是正确的。愚蠢的问题,但您希望功能的内容位于 Stlye 部分?你看过页面背后的来源吗?

标签: php wordpress wordpress-theming


【解决方案1】:

请在调用get_posts()函数之前添加global $post;,并且不要在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】:

    不确定category=3 是否有效,但请在您的get_posts 语句中使用cat=3

    在你的 foreach 语句之后还需要setup_postdata($post);

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多