Php 显示选定产品或产品类别的html
您好,我正在使用高级自定义字段插件,在带有关系选择器的选项页面中,仅显示选定产品或选定类别产品的某些html。我的结果是显示所有产品。这就是我所拥有的:Php 显示选定产品或产品类别的html,php,advanced-custom-fields,acfpro,Php,Advanced Custom Fields,Acfpro,您好,我正在使用高级自定义字段插件,在带有关系选择器的选项页面中,仅显示选定产品或选定类别产品的某些html。我的结果是显示所有产品。这就是我所拥有的: $products=get_字段('products_-picker','option'); $categories=get_字段('categories_picker','option'); $prom_img=get_字段('prom_img','option'); 如果($产品){ foreach(产品为$p): 如果($post->I
$products=get_字段('products_-picker','option');
$categories=get_字段('categories_picker','option');
$prom_img=get_字段('prom_img','option');
如果($产品){
foreach(产品为$p):
如果($post->ID==$p->ID):
">
endif;
endforeach;
}
如果($类别){
foreach(类别为$term):
$category=get_term($term);
if(有术语($category,$product_cat',$post)){
?>
">
endif;
endforeach;
}
在类似的情况下,我创建了一个新的wp\u查询,该查询使用post ID传递数组,如下所示:
$posts = get_field('products_picker', 'option');
$new_query = new WP_Query(array(
'post_type' => array('post'),
'post__in' => $posts,
'orderby' => 'post__in',
));
if ( $new_query->have_posts() ) :
while ( $new_query->have_posts() ) : $new_query->the_post();
//your code here
endwhile;
endif;
对于“类别”零件,可以使用如下查询:
$categories = get_field('categories_picker', 'option');
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories[0]
)
)
);
$cat_query = new WP_Query($args);
我没有在Wordpress中测试过这一点,但我要尝试的第一件事是重命名$post变量,该变量将ACF字段拉至“$posts”(复数) 然后按如下所示更改foreach循环
$posts=get_字段('products_picker','option');
$prom_img=get_字段('prom_img','option');
如果有的话(员额){
foreach($post为$post):
设置_postdata($post);
$sales_html='';
wp_reset_postdata();
endforeach;
}
谢谢您的回答,但使用产品类别页面上的此代码,所有产品都将替换为关系选择器中选择的第一个产品,并添加html/图像($prom\u img,在我的示例中)我不确定我是否正确理解了这个问题。你不想在结果页面中筛选这些帖子,但你想在当前的“单一产品页面”或“归档页面”中显示一些内容,对吗?不,我不想过滤它们,我只想在选择器中显示所选产品的html。实际上,html是一个图像标记,将显示在产品图像的顶部。因此,它将显示在产品类别页面和单个产品页面上。我还有另一个选择器,可以在其中选择产品因此,如果选择此项,html将显示在所选类别的所有产品上。如果是这样,我认为您可以通过在表$posts=get_字段('products_picker','option')中搜索当前ID
来实现您的目标
如果存在,则显示img
嘿,我为产品选取器修复了代码,我编辑了代码,但无法使其用于类别选取器