Wordpress get_post_meta在我的wp模板中不起作用

Wordpress get_post_meta在我的wp模板中不起作用,wordpress,Wordpress,首先,我在functions.php文件中注册自定义帖子。哪个名称promo…然后我在index.php中使用了代码,并在自定义字段中使用了值…但是标题和内容有效,而get\u post\u meta无效。$post必须存在。。。您必须在循环中使用get\u post\u meta,或者通过其他方式获取帖子ID在设置全局$post;之前,您正在调用get\u post\u meta$post->ID[…];。谢谢现在工作@GeraldSchneider和mathielo <?php $pr

首先,我在functions.php文件中注册自定义帖子。哪个名称promo…然后我在index.php中使用了代码,并在自定义字段中使用了值…但是标题和内容有效,而get\u post\u meta无效。

$post必须存在。。。您必须在循环中使用get\u post\u meta,或者通过其他方式获取帖子ID在设置全局$post;之前,您正在调用get\u post\u meta$post->ID[…];。谢谢现在工作@GeraldSchneider和mathielo
<?php $promo_icon = get_post_meta($post->ID, 'promo_icon', true);?>
        <?php
        global $post;
        $args=array( 'posts_per_page'=> 4, 'post_type'=>'promo');
        $myposts=get_posts($args);
        foreach($myposts as $post): setup_postdata($post);?>
            <li>
                <a href="#">
                <span class="ca-icon">
                <?php echo $promo_icon; ?>
                </span>
                <div class="ca-content">
                    <h2 class="ca-main"><?php the_title(); ?></h2>
                    <h3 class="ca-sub"><?php the_content(); ?></h3>
                </div>
                </a>
            </li>
        <?php endforeach; ?>