在函数内部查询$ out

时间:2015-09-28 13:53:30

标签: php wordpress 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,任何人都可以帮忙?

1 个答案:

答案 0 :(得分:0)

您需要创建单独的变量。并且你使用它。在启动变量之前准备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;