查询函数内部$out


Query inside $out inside a function

我有问题在我的functions.php文件内运行查询。我有相当广泛的功能,它返回一组基于若干设置参数的帖子。

我现在需要在函数循环中运行一个循环来给我另一个结果。

如果我在一个正常的页面中这样做,它看起来像这样,工作正常:

<div class="brief">
    <a href="<?php the_permalink(); ?>"><?php the_title() ?></a>    
    <?php 
        $terms = get_the_terms( $post->ID, 'supplier-tax');
        foreach ( $terms as $term ) {
            $termID[] = $term->term_id;
        }
        $the_query = new WP_Query( array(
         'post_type' => 'supplier',
         'tax_query' => array(
             array(        
                'taxonomy' => 'supplier-tax',
                'terms' => $termID,
              )
            ),
        ) );
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <p class="suppy"><?php the_title(); ?></p>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <p class="hide-for-small-only"><?php echo get_the_excerpt(); ?></p>
    <a class="more-btn" href="<?php the_permalink(); ?>">More</a>
</div>                  
</div>

但是,如果我想在我的函数的$out部分输出这个,它不起作用,这是我的最新尝试:

$out .= '<div class="small-12 large-6 columns end thumb under" data-equalizer-watch>
            <div id="case">
                <div class="th" id="element" style="background-image: url('.get_field("product_image", false, true).')">
                    <a class="fill-div" href="'. get_permalink($post->ID) .'"></a>
                </div>
            </div>

            <div class="brief">
                <a href="'. get_permalink($post->ID) .'">'.get_the_title().'</a>    

                '$terms = get_the_terms( $post->ID, 'supplier-tax');
                foreach ( $terms as $term ) {
                    $termID[] = $term->term_id;
                }
                $the_query = new WP_Query( array(
                 'post_type' => 'supplier',
                 'tax_query' => array(
                     array(        
                        'taxonomy' => 'supplier-tax',
                        'terms' => $termID,
                      )
                    ),
                ) );
                while ( $the_query->have_posts() ) : $the_query->the_post();'
                <p class="suppy">'.get_the_title().'</p>
                'endwhile;'
                'wp_reset_postdata();'
                <p class="hide-for-small-only">'.get_the_excerpt().'</p>
                <a class="more-btn" href="'. get_permalink($post->ID) .'">More</a>
            </div>                  
        </div>';

根据调试,我有意想不到的$terms,这是该代码第9行查询的第一行,但我不知道我还需要如何添加$条款,有人能帮助吗?

您需要的是创建单独的变量。然后你用它。在初始化变量之前准备好terms参数

试试这个。

$backgroundImg = get_field("product_image", false, true);
$permalink = get_permalink($post->ID);
$postTitle = get_the_title($post->ID);
$postExcerpt = get_the_excerpt($post->ID);
$terms = get_the_terms($post->ID, 'supplier-tax');
foreach ($terms as $term) {
    $termID[] = $term->term_id;
}
$the_query = new WP_Query(array(
    'post_type' => 'supplier',
    'tax_query' => array(
        array(
            'taxonomy' => 'supplier-tax',
            'terms' => $termID,
        )
    ),
));
$out = '<div class="small-12 large-6 columns end thumb under" data-equalizer-watch>
            <div id="case">
                <div class="th" id="element" style="background-image: url('.$backgroundImg.')">
                    <a class="fill-div" href="'. $permalink .'"></a>
                </div>
            </div>
            <div class="brief">
                <a href="'. $permalink .'">'.$postTitle.'</a>';
                while ( $the_query->have_posts() ) : $the_query->the_post();
                    $out .= '<p class="suppy">'.get_the_title().'</p>';
                endwhile;
                wp_reset_postdata();
                $out .= '<p class="hide-for-small-only">'.$postExcerpt.'</p>
                <a class="more-btn" href="'. $permalink .'">More</a>
            </div>
        </div>';
echo $out;