Php foreach,带有$posts和$terms

Php foreach,带有$posts和$terms,php,wordpress,foreach,advanced-custom-fields,Php,Wordpress,Foreach,Advanced Custom Fields,我在Wordpress网站上使用字段,以便管理员用户可以轻松确定哪些帖子在该页面上可见。我有一个自定义分类法,我需要能够获取\u术语并打印每篇文章的术语。这通常是通过中提到的foreach实现的 但是,我使用foreach获取$posts,因此我不确定如何使用它同时打印我的H3中的术语名称和主中的术语slug 代码如下: <?php $posts = get_field('team_members',12); $terms = get_the_terms( $post->ID , '

我在Wordpress网站上使用字段,以便管理员用户可以轻松确定哪些帖子在该页面上可见。我有一个自定义分类法,我需要能够
获取\u术语
并打印每篇文章的术语。这通常是通过中提到的foreach实现的

但是,我使用foreach获取
$posts
,因此我不确定如何使用它同时打印我的
H3
中的术语名称和主
中的术语slug

代码如下:

<?php
$posts = get_field('team_members',12);
$terms = get_the_terms( $post->ID , 'position' );
if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) 
    setup_postdata($post); ?>
    <div class="col-4 col <?php echo $term->slug;?>">
        <article id="post-<?php the_ID(); ?>" <?php post_class('team-item'); ?>>
            <hgroup>
                <?php the_title( sprintf( '<h2 class="alt-heading-4">', esc_url( get_permalink() ) ), '</h2>' ); ?>
                <h3><?php echo $term->name;?></h3>
            </hgroup>

            <div class="team-entry-content">
                <?php the_content();?>
            </div><!-- .entry-content -->
            <div id="team-shadow"></div>
        </article><!-- #post-## -->
    </div>
     <?php endforeach; ?>
    <?php wp_reset_postdata();?>
<?php endif; ?>


由于条款与您的帖子相关联,您必须放置:

$terms = get_the_terms( $post->ID , 'position' );
在foreach循环内部,在外部它将根本不工作,因为
$post->ID
将出错:

trying to get the property of non object
因此,解决方案是采用
$terms=get_the_terms($post->ID,'position')并将其添加到foreach循环中:

<?php
$posts = get_field('team_members',12);
if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) 
    setup_postdata($post);
       $terms = get_the_terms( $post->ID , 'position' ); ?>
    <div class="col-4 col <?php echo $term->slug;?>">
        <article id="post-<?php the_ID(); ?>" <?php post_class('team-item'); ?>>
            <hgroup>
                <?php the_title( sprintf( '<h2 class="alt-heading-4">', esc_url( get_permalink() ) ), '</h2>' ); ?>
              <?php foreach($terms as $term) {?>
                <h3><?php echo $term->name;?></h3>
              <?php } ?>
            </hgroup>

            <div class="team-entry-content">
                <?php the_content();?>
            </div><!-- .entry-content -->
            <div id="team-shadow"></div>
        </article><!-- #post-## -->
    </div>
     <?php endforeach; ?>
    <?php wp_reset_postdata();?>
<?php endif; ?>


因此,我不需要阅读大量文档,是在
get_theu terms()
调用中使用的
$post->ID
$post
迭代中定义的
$post
相关,还是来自早期代码的
$post
变量?换句话说,您是否正在为从
get\u field()
循环的每个post
ID
检索术语?$post->ID不用于get\u术语中。一切都将保持原样,除了foreach由于不熟悉WP而看起来像‘foreach($terms as$term)’,我不完全确定其意图,但在我看来,您需要在外部
foreach($post作为$post)
中嵌套另一个
foreach($terms作为$term)
,其中,您将调用移动到$posts
foreach
循环中,而不是在它之前。但正如我所说,我对WP不熟悉……我的意思是:
$posts=get_field()…foreach($posts as$post){$terms=get_The_term()…foreach($terms as$term){//print term info}
@probablybely很高兴它能帮到你:)。我发现了一个奇怪的错误,第一篇文章没有得到$term->slug或$term->name。知道是什么引起的吗?从第二篇文章中,它得到了条款。作为第一篇文章,我试着用不同的术语创建不同的文章,但没有任何区别。@probablybest似乎第一篇文章可能不包含术语。definitely包含术语。我已经试着把第一篇文章改成我知道有效的文章,并修改了术语,但没有任何区别。@probablybest我确实编辑了我的答案,并尝试使用新的更新代码