我需要更改它以仅显示类别ID中的产品

时间:2015-03-22 08:45:12

标签: php wordpress

基本上我需要此代码才能仅显示来自 类别ID的产品。

来自woocomerrce模板。

任何帮助将不胜感激!



 <div class="book_wrapper" <?php echo (!empty($book_wrapper)) ? 'style="background-image:url('.esc_url($book_wrapper).');"' : ''; ?>>
                <a id="next_page_button"></a>
                <a id="prev_page_button"></a>
                <div id="loading" class="loading"><?php _e('Loading pages!', THEME_NAME); ?>...</div>
                <div id="mybook" style="display:none;">
                    <div class="b-load">
                        <?php
                           $args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
                           $loop = new WP_Query( $args );
                           $counter = 0;
                           echo "<div><ul>";
                           if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
                           <?php if ($counter == 8): ?>
                           	<?php echo '</ul></div><div><ul>'; $counter = 0; ?>
                           <?php endif ?>
                                <li>
	                                <a href="<?php the_permalink(); ?>">
	                                    <div class="meal-name"><?php the_title(); ?></div>
	                                    <div class="meal-price"><?php echo $product->get_price_html(); ?></div>
	                                </a>
                                </li>
                        <?php $counter++; endwhile; endif; ?>
                        <?php echo '</ul></div>'; ?>
                    </div>
                </div>
            </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

检查此网址以获取更多信息https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters 您需要在WP_Query的参数中添加具有特定类别的cat。

&#13;
&#13;
 <div class="book_wrapper" <?php echo (!empty($book_wrapper)) ? 'style="background-image:url('.esc_url($book_wrapper).');"' : ''; ?>>
                <a id="next_page_button"></a>
                <a id="prev_page_button"></a>
                <div id="loading" class="loading"><?php _e('Loading pages!', THEME_NAME); ?>...</div>
                <div id="mybook" style="display:none;">
                    <div class="b-load">
                        <?php
                           $args = array( 'post_type' => 'product', 'posts_per_page' => -1, cat=> 'Replace with cat id here' );
                           $loop = new WP_Query( $args );
                           $counter = 0;
                           echo "<div><ul>";
                           if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
                           <?php if ($counter == 8): ?>
                           	<?php echo '</ul></div><div><ul>'; $counter = 0; ?>
                           <?php endif ?>
                                <li>
	                                <a href="<?php the_permalink(); ?>">
	                                    <div class="meal-name"><?php the_title(); ?></div>
	                                    <div class="meal-price"><?php echo $product->get_price_html(); ?></div>
	                                </a>
                                </li>
                        <?php $counter++; endwhile; endif; ?>
                        <?php echo '</ul></div>'; ?>
                    </div>
                </div>
            </div>
&#13;
&#13;
&#13;