我在一个Wordpress站点上使用ACF关系字段,这样管理员用户就可以很容易地确定在该页面上哪些帖子是可见的。我有一个定制的分类法,我需要能够get_the_terms和打印每个帖子的术语。正如在法典中提到的那样,这是通过预先实现的。
然而,我使用这个程序来获取$posts,所以我不知道如何使用它在我的H3中打印术语名称,在主<div>中打印so一词
代码如下:
<?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; ?>发布于 2015-11-09 22:54:54
由于这些条款与您的职位相关联,您必须将:
$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; ?>我希望它确实有帮助:)。
https://stackoverflow.com/questions/33613157
复制相似问题