无法获取具有特定term_id的发布数据

时间:2019-04-20 06:26:53

标签: wordpress-theming

我正在使用引导程序来做一个嵌套的选项卡,所以我想用特定的term_id本身查询帖子数据。请查看我的代码我在做什么错。 我得到的结果只是term_id的第一个,而另一个term_id没有得到结果。 在我的情况下,我认为循环仅工作一次。

Here is the result Here is the result enter image description here

<!-- Nav tabs -->          
                            <ul class="nav nav-tabs" id="myTab-<?php echo $cat->slug?>" role="tablist">
                                <?php 
                                $args = array('child_of' => $cat->term_id);
                                $categories = get_categories( $args );
                                //print_r($categories);
                                $ix = 1;
                                foreach($categories as $category) :?>                           
                                <li class="nav-item">
                                    <a class="nav-link <?php echo $ix?> <?php if($ix==1){echo "active";}?>" id="tab-<?php echo $category->term_id?>" data-toggle="tab" href="#tab-<?php echo $category->term_id.$ix?>" role="tab" aria-controls="tab-<?php echo $category->term_id.$ix?>" aria-selected="<?php if($ix==1){echo "true";}else{echo "false";}?>"><?php echo $category->name;?></a>
                                </li>                
                                <?php $ix++; endforeach ?>            
                            </ul>

                                <div class="tab-content">
                                    <?php
                                    $ix = 1; 
                                    foreach($categories as $category) :?>  
                                        <div class="tab-pane <?php echo $ix?> <?php if($ix==1){echo "active";}?>" id="tab-<?php echo $category->term_id.$ix?>" role="tabpanel" aria-labelledby="tab-<?php echo $category->term_id.$ix?>">
                                            <?php echo "term_ID: ". $category->term_id?>

                                            <ul class="row child_of_cat">                          
                                            <?php
                                                $qr     = new WP_Query( $args = array(
                                                'post_type'             => 'services',
                                                'parent'                => 1,
                                                'post_status'           => 'publish',
                                                'ignore_sticky_posts'   => 1,
                                                'posts_per_page'        => -1, // Limit: two products
                                                //'post__not_in'          => array( get_the_id() ), // Excluding current product
                                                'tax_query'             => array( array(
                                                    'taxonomy'      => $taxonomy,
                                                    'field'         => 'term_id', // can be 'term_id', 'slug' or 'name'
                                                'terms'         => $category->term_id,
                                                ), ),
                                            ));



                                            ?>
                                            <?php
                                                if ( $qr->have_posts() ): 
                                                    while( $qr->have_posts() ): 
                                                        $qr->the_post();

                                                        if(get_the_post_thumbnail($qr->post->ID) ==''){
                                                            $img = '<img class="img-fluid" src="https://via.placeholder.com/300/ddd/fff.png">';                               
                                                        } else{
                                                            $img = get_the_post_thumbnail($qr->post->ID);
                                                        }

                                                        echo '<li class="col-6 col-sm-3">
                                                            <div class="loop-box">';
                                                            if (has_term($term = 'best-seller', $taxonomy = 'collection', $post = $qr->post->ID)){
                                                                echo '<img class="conner-bage-bg" src="'.get_template_directory_uri().'/img/bage_new.png">';
                                                            }
                                                        echo    '<a href="'.get_permalink().'">'.$img.'</a>
                                                                <a href="'.get_permalink().'"><div class="description">
                                                                    <p class="woocommerce-loop-product__title">'.$qr->post->post_title.'xx</p>
                                                                </div></a>
                                                            </div>
                                                        </li>';
                                                    endwhile;

                                                    //wp_reset_postdata();
                                                    rewind_posts();
                                                endif; ?>
                                            </ul>                     
                                        </div><!-- tab-pane -->
                                    <?php $ix++; endforeach ?>
                                </div>

1 个答案:

答案 0 :(得分:0)

我已经使用get_posts()对其进行了修复。

enter link description here