我试图在WooCommerce中查询产品,并将其ID作为一个数组返回,该数组可用于有条件过滤。我使用ACF Pro向产品中添加自定义字段,然后使用条件代码。
我和我的开发人员朋友谈过,他建议采用这种方法,将数组存储在$ex_id或$inc_id变量中;
<?php
query_posts( $args ); while ( have_posts() ) : the_post(); ?>
<?php if( in_array( \'yes\', get_field(\'exclude\') ) ) { ?>
<?php $ex_id[] = the_id() ;?>
<?php } else if( in_array( \'yes\', get_field(\'include\') ) ) { ?>
<?php $inc_id[] = the_id() ;?>
<?php } ;?>
<?php endwhile; wp_reset_query(); ?>
这不起作用,所以我简化了条件代码,发现返回id是首页的id;<?php
query_posts(); while ( have_posts() ) : the_post(); ?>
<?php the_id() ;?>
<?php endwhile; wp_reset_query(); ?>
如果我添加$args(特别是;\'post\\u type\'=>\'product\',\'showposts\'=>-1),则查询有效并返回正确的ID,只有它们会打印在屏幕上。有人知道我如何查询和存储为变量吗?