带有 $posts 和 $terms 的 foreach
foreach with $posts and $terms
我正在使用 ACF relationship field on a Wordpress site so that the admin user can easily determine which posts are visible on that page. I have a custom taxonomy where I need to be able to get_the_terms
and print the term for each post. This is normaly achieved with an foreach as mentioned in the codex。
但是我使用 foreach 来获取 $posts
所以我不确定如何使用它来打印我的 H3
中的术语名称和主 [=] 中的术语 slug 14=]
代码如下:
<?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; ?>
由于条款与您的 post 相关联,因此您必须放置 :
$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; ?>
希望对您有所帮助:)。
我正在使用 ACF relationship field on a Wordpress site so that the admin user can easily determine which posts are visible on that page. I have a custom taxonomy where I need to be able to get_the_terms
and print the term for each post. This is normaly achieved with an foreach as mentioned in the codex。
但是我使用 foreach 来获取 $posts
所以我不确定如何使用它来打印我的 H3
中的术语名称和主 [=] 中的术语 slug 14=]
代码如下:
<?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; ?>
由于条款与您的 post 相关联,因此您必须放置 :
$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; ?>
希望对您有所帮助:)。