试图获取分类法术语名称时出错



你好,所以我试图从2个分类法术语中获得2个帖子,我已经完成了那部分,我正在努力在帖子上方显示术语名称。在使用foreach循环之前我已经这样做了,但是无论我做了什么改变,我总是得到我为循环提供了无效参数的错误。在谷歌上搜索了很多之后,我有点迷路了,不知道你们是否能给我一些指导?

$current_post_id = get_posts( array(
        'post_type'      => 'issue',
        'posts_per_page' => 1,
        'fields'         => 'ids',
    ) );
    $terms = get_the_terms( $current_post_id, 'department' );
        foreach ( $terms as $term ) {
            $args = array(
                'connected_type'      => 'posts_to_issues',
                'connected_direction' => 'to',
                'connected_items'     => $current_post_id,
                'post_type'           => 'post',
                'posts_per_page'      => 2,
                'tax_query'           => array(
                    array(
                        'taxonomy' => 'department',
                        'field'    => 'slug',
                        'terms'    => array( 'washington-watch', 'equipment-spotlight'),
                    )
                ),
            );
            $sidebar_query = new WP_Query( $args ); 
            while ( $sidebar_query->have_posts() ) : $sidebar_query->the_post(); ?>

           <?php echo '<h2>' . $term->name . '</h2>' ?>
            <figure>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail( 'sidebar-thumb-med' ); ?>
                </a>
             </figure>
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <?php the_excerpt();
        endwhile;
        }
        // Restore global post data
        wp_reset_query();  

$current_post_id是一个只有一项的数组(因为传递一个返回fields参数返回一个数组)。在下列情况下,您需要参考那一项:

$terms = get_the_terms( $current_post_id[0], 'department' );

……

'connected_items' => $current_post_id[0],

最新更新