【问题标题】:Display html for selected products or products categories显示选定产品或产品类别的 html
【发布时间】:2020-06-10 05:17:51
【问题描述】:

您好,我正在使用高级自定义字段插件仅显示某些 html 用于选定产品或仅用于带有关系选择器的选项页面中选定类别的产品。相反,我的结果是显示所有产品。这就是我所拥有的:

$products = get_field('products_picker', 'option');
$categories =  get_field('categories_picker', 'option');
$prom_img = get_field('prom_img', 'option');


if ( $products ) {

	foreach( $products as $p ):
	
       if( $post->ID == $p->ID ):

 	<img src="<?php echo  $prom_img['url']; ?>">


    endif;
	endforeach;
		}

 if ( $categories ) {

	foreach( $categories as $term ):
	
       $category = get_term( $term );
    
   if( has_term( $category, 'product_cat', $post )) {
      ?>

 <img src="<?php echo  $prom_img['url']; ?>">

    endif;
	endforeach;
		}

【问题讨论】:

    标签: php advanced-custom-fields acfpro


    【解决方案1】:

    在类似的情况下,我创建了一个新的 wp_query,并使用下面的帖子 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);
    

    【讨论】:

    • 感谢您的回答,但使用产品类别页面上的此代码,所有产品都将替换为关系选择器中选择的第一个产品,并多次添加 html/ 图像(在我的示例中为 $prom_img)该类别中的产品数量
    • 我不确定我是否正确理解了这个问题。您不想在结果页面中过滤这些帖子,但您想在他们当前的“单个产品页面”或“存档页面”中显示一些内容,对吗?
    • 不,我不想过滤它们,我只想为选择器中的选定产品显示该 html。实际上,该 html 是一个图像徽章,将显示在产品图像的顶部。因此它将显示在产品类别页面和单个产品页面上。我还有另一个选择器,我可以在其中选择产品类别。因此,如果选择此选项,则 html 将显示在所选类别的所有产品上。
    • 如果是这样,我认为您可以通过在表$posts = get_field('products_picker', 'option'); 中搜索current ID 来实现您的目标,如果存在则显示img
    • 嘿,我修复了产品选择器,我编辑了我的代码,但我无法让它为类别选择器工作
    【解决方案2】:

    我没有在 Wordpress 中对此进行过测试,但我会尝试的第一件事是将用于拉 ACF 字段的 $post 变量重命名为“$posts”(复数)。

    然后改变foreach循环如下。

    $posts = get_field('products_picker', 'option');
    $prom_img = get_field('prom_img', 'option');
    
    
    if ( $posts ) {
    
    	foreach( $posts as $post ):
    
    		setup_postdata($post);
    			
    $sales_html = '<div class="promo-badge test"><img src=' . $prom_img['url'] . '></div>';
    
    		wp_reset_postdata();
    	endforeach;
    		}

    这可能会解决它...我不确定您是否需要将 foreach 循环转换为实际的 Wordpress 样式循环(带有while(have_posts()): the_post(); 等的类型,如下面的@kaize 答案)

    如果我的解决方案没有帮助,请查看此处:https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多