【问题标题】:foreach with $posts and $terms带有 $posts 和 $terms 的 foreach
【发布时间】:2016-02-10 08:05:09
【问题描述】:

我在 Wordpress 网站上使用 ACF relationship 字段,以便管理员用户可以轻松确定该页面上可见的帖子。我有一个自定义分类法,我需要能够get_the_terms 并打印每个帖子的术语。这通常通过 codex 中提到的 foreach 来实现。

但是我使用 foreach 来获取 $posts,所以我不确定如何使用它来打印我的 H3 中的术语名称和主 <div> 中的术语 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; ?>

【问题讨论】:

  • 所以我不需要阅读大量文档,get_the_terms() 调用中使用的$post-&gt;ID 是否与foreach ($posts as $post) 迭代中定义的$post 相关,或者是@987654332 @ 来自早期代码的变量?换句话说,您是否正在检索从 get_field() 循环的每个帖子 ID 的术语?
  • $post->ID 未在 get_the_terms 中使用。一切都会保持原样,除了 foreach 看起来像 'foreach($terms as $term)'
  • 由于对 WP 不熟悉,我并不能完全确定其意图,但在我看来,您需要在外部 foreach ($posts as $post) 内部执行的操作是另一个嵌套在其中的 foreach ($terms as $term),其中您将调用移动到 get_the_terms() 以发生 inside $posts foreach 循环而不是在它之前。但正如我所说,我对 WP 不熟悉....
  • 我的意思是算法:$posts = get_field()...foreach($posts as $post) { $terms = get_the_term()... foreach ($terms as $term) { //print term info } }

标签: php wordpress foreach advanced-custom-fields


【解决方案1】:

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

$terms = get_the_terms( $post->ID , 'position' );

在 foreach 循环内部,在外部它根本不起作用,因为 $post-&gt;ID 将是错误的:

trying to get the property of non object

所以解决方案是将$terms = get_the_terms( $post-&gt;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; ?>

我希望它确实有所帮助:)。

【讨论】:

  • @probablybest 很高兴它帮助了你:)。
  • 我发现了一个奇怪的错误,即第一篇文章没有得到 $term->slug 或 $term->name。知道是什么原因造成的吗?然后从第二篇文章中获取条款。我尝试用不同的术语创建不同的帖子作为第一篇帖子,但没有任何区别。
  • @probablybest 似乎第一篇文章可能不包含术语。
  • 肯定有条款。我尝试将第一个帖子更改为我知道有效的帖子并更改条款,但没有任何区别。
  • @probablybest 我确实编辑了我的答案尝试使用新的更新代码
猜你喜欢
  • 2020-08-14
  • 2021-03-07
  • 2013-03-01
  • 2013-10-16
  • 2014-04-26
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多