【问题标题】:how to get value of wysiwyg custom field (ACF) for product category on wordpress custom page如何在 wordpress 自定义页面上获取产品类别的所见即所得自定义字段 (ACF) 的值
【发布时间】:2019-05-23 17:59:04
【问题描述】:

我正在创建一个自定义页面来列出所有产品 (taxonomy=product_cat) 子类别以及我创建的 ACF 字段。

我尝试在谷歌上搜索解决方案,但我只能得到 the_field('');

但是当我调用它时,它没有显示自定义字段中的任何值是所见即所得

<?php 
$parentid = get_queried_object_id();         
$args = array(
    'parent' => 68,
     'hierarchical' => 1,
     'show_option_none' => '',
     'hide_empty' => 0,
);
$terms = get_terms( 'product_cat', $args );

if ( $terms ) {
        foreach ( $terms as $term ) {
               var_dump($term);
               echo '<div class="row expand-blocks"><div class="col-md-3">
               <h3 class="category_name">'.$term->name.'</h3>';
               woocommerce_subcategory_thumbnail( $term );
               echo'</div> <div class="col-md-9 category_description"><p class="readmoretoggle">'.$term->description.'</p> <div class="row category_full_description"><div class="">';
echo '<div class="col-md-6"><div class="nutrition_value ">';

                    //need to show below                    
                      the_field('nutrition_value_per_one_cup', '107');

                    echo '</div></div> <div class="col-md-6"><div class="useful_value ">';

                    //need to show below
                     the_field('useful_for_body_parts');

                    echo '</div> </div>';

    }


}

?>

我希望字段的 html 输出

【问题讨论】:

    标签: wordpress advanced-custom-fields wysiwyg


    【解决方案1】:

    您可以在此页面上使用参考。 https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    //where $term = term object you get in foreach loop
    the_field('nutrition_value_per_one_cup', $term);// display the value itself
    or
    echo get_field('nutrition_value_per_one_cup', $term);// need to echo or print the value.
    
    

    【讨论】:

      【解决方案2】:

      the_field('field_name') 是根据documentation 在主题中显示 acf 字段的正确方法。

      在您的代码中,我看到了该函数的两种用法:

      the_field('nutrition_value_per_one_cup', '107');
      
      the_field('useful_for_body_parts');
      

      哪个不对? 你得到的当前输出是多少?

      另外,当您处于循环中时,为什么要在第一个值上强制将 prost_id 值设置为 107

      您是否尝试强制使用该术语的 post_id?:

      the_field('your_field_name', $term->id);
      

      可能是 $term->ID,我不确定。

      【讨论】:

      • 这两种格式都是正确的,我需要将其设置为有条件的,以便我可以根据 the_field('useful_for_body_parts', $term->term_id);我当前的条件是 taxonomy=product_cat 我正在尝试在自定义页面模板上显示它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 2012-03-06
      • 2018-12-24
      • 1970-01-01
      相关资源
      最近更新 更多