dongxiansi0158 2015-09-28 13:53
浏览 29
已采纳

在函数内部查询$ out

I'm having issues running a query inside of my functions.php file. I have quite an extensive function which returns a set of posts based upon a number of set parameters.

I need to now run a loop inside of the functions loop to give me another result.

If i do this in a normal page it looks like this and works fine:

<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>

However if i want to output this in the $out section of my function, it does not work, here is my latest attempt:

$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>';

According to debug I have unxepected $terms which is the first line of the query on line 9 of that code, however i have no idea how else i would need to add the $terms can anyone help?

  • 写回答

1条回答 默认 最新

  • dou11655853 2015-09-28 14:18
    关注

    What you need is to create separate variables. and your use it. prepare the terms arguments before initiating the variable.

    Try this.

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题