如何获取自定义 post 类型 ID
How to get the custom post type id
我有一个自定义 post 类型的产品页面,现在我想要该特定页面的类别 ID 并将其放在 post 循环中。并用它创建一个光滑的滑块。
这是我想出来的。
单品页面
<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
它向您显示相同的 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);
希望对您有所帮助。 :)
我是如何使用 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);
我有一个自定义 post 类型的产品页面,现在我想要该特定页面的类别 ID 并将其放在 post 循环中。并用它创建一个光滑的滑块。
这是我想出来的。
单品页面
<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
它向您显示相同的 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);
希望对您有所帮助。 :)
我是如何使用 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);