Skip to content

Instantly share code, notes, and snippets.

@Prroffessorr
Last active January 18, 2020 17:26
Show Gist options
  • Save Prroffessorr/526d36ad01f368a446f490bcb0a5bb3f to your computer and use it in GitHub Desktop.
Save Prroffessorr/526d36ad01f368a446f490bcb0a5bb3f to your computer and use it in GitHub Desktop.
Wordpress(Woocommerce):Пример вывода товаров в сайдбаре, которые принадлежат той же категории что и выбранный
<?php
$product_cat_slugs = array();
foreach (get_the_terms($product->ID, 'product_cat') as $term) {
array_push($product_cat_slugs, $term->slug);
}
$args = array(
'posts_per_page' => carbon_get_theme_option('numbers'),//or carbon_get_the_post_meta('numbers')//or just number
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_cat_slugs
),
),
'post_type' => 'product',
'orderby' => 'title',
);
?>
<div class="sidebar_block">
<?php
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) {
$the_query->the_post();?>
<ul>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php }
wp_reset_postdata();
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment