我有一个产品页面,该页面是自定义帖子类型,现在我想要该特定页面的类别ID并将其放在帖子循环中。并使用它创建一个光滑的滑块。
这就是我想出的。
单个产品页面
<section class="product">
<div class="productslider">
<?php
global $post;
$Taxonomy = get_object_taxonomies('producten');
if (count($Taxonomy) > 0) {
foreach ($Taxonomy as $tax) {
$args = array(
'taxonomy' => $tax,
);
$cats = get_categories($args);
$cats = $cats[0]->term_id;
}
}
$args = array('posts_per_page' => 5, 'post_type' => 'producten', 'category' => $cats);
$posts = get_posts($args);
if ($posts) :
foreach ($posts as $post) :
setup_postdata($post); ?>
<div class="productslider__slide">
<?php echo get_the_post_thumbnail(); ?>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
</div>
<?php endforeach;
wp_reset_postdata();
endif; ?>
</div>
</section>
光滑
$('.productslider').slick({
slidesToShow: 3,
slidesToScroll: 1,
autoplay: true,
autoplaySpeed: 2000,
arrows: false,
dots: true,
centerMode: true,
});
这是一个好的解决方案吗? 好像我在每个页面上都得到了相同的ID
答案 0 :(得分:1)
当您从循环中获得相同的ID时,它会向您显示相同的ID,
这是您的代码
$cats = $cats[0]->term_id;
以上一行仅从循环中获取第一个ID,并且您在get_posts()
上使用此类别ID,因此每次都将获得相同的ID。
尝试修改此代码:
$cat_array = array();
$cats = get_categories($args);
$cat_array[] = $cats->term_id;
$args = array('posts_per_page' => 5, 'post_type' => 'producten', 'category' => $cat_array);
希望它会对您有所帮助。 :)
答案 1 :(得分:0)
我如何使用Manan Vyas答案解决问题
$Taxonomy = get_object_taxonomies('producten');
if (count($Taxonomy) > 0) {
foreach ($Taxonomy as $tax) {
$args = array(
'taxonomy' => $tax,
);
$cat_array = array();
$cats = get_categories($args);
foreach($cats as $data) {
array_push($cat_array, $data->term_id);
}
}
}
$args = array('posts_per_page' => 5, 'post_type' => 'producten', 'category' => $cat_array);