【发布时间】:2021-08-27 06:04:53
【问题描述】:
目前,我正在一个网站上工作,我必须在其中显示使用分类字段选择的类别的帖子;我为此使用高级自定义字段。使用“普通”(单个)帖子和自定义帖子类型,它就像一个魅力。展示它是如何工作的:
<?php
// get the current taxonomy term
$term = get_queried_object();
$catact = get_field('actueel_category', $term);
$loop = new WP_Query( array(
'post_type' => 'actueel',
'posts_per_page' => 2,
'category__in' => $catact,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink();?>">
<div class="post">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div class="thumbnail" style="background-image:url('<?php echo $thumb['0'];?>');">
</div>
<div class="theme__inner__content">
<h4><?php the_title();?></h4>
<span class="more">lees meer</span>
</div>
</div>
</a>
<?php endwhile; wp_reset_query(); ?>
现在,当我尝试对 Woocommerce 产品做同样的事情时,它不起作用。这是我使用的代码:
<?php
// get the current taxonomy term
$term = get_queried_object();
$catpro = get_field('product_category', $term);
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 2,
'product_cat' => $catpro,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink();?>">
<div class="post">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div class="thumbnail" style="background-image:url('<?php echo $thumb['0'];?>');">
</div>
<div class="theme__inner__content">
<h4><?php the_title();?></h4>
<span class="more">lees meer</span>
</div>
</div>
</a>
<?php endwhile; wp_reset_query(); ?>
有什么我没有得到的吗?
在管理区域:我使用分类字段,两个输出都显示为术语 ID。对于常规帖子,我选择了“类别”分类法,为产品选择了“product_cat”分类法。
有人可以在这里和我一起思考吗?我似乎无法解决它。也许我忽略了一些东西。
提前致谢!
【问题讨论】:
-
嗨。为什么使用 get_field()?您使用的是高级自定义字段插件吗?
-
@AdrianaHernández 是的!对不起,忘了提这个。编辑了我的帖子。
标签: php wordpress woocommerce acfpro