如何在 WordPress functions.php 中一起使用函数和 HTML

How to use function and HTML together in functions.php in WordPress

我有一个问题。请帮助我更新鲜,我需要一个指南。

我必须使用该函数来获取自定义字段值,并且需要在我的锚点中设置该值 link。但是 link

之外的打印
// add_action( 'woocommerce_single_product_summary', 'technical_specification_pdf_url', 32 );
function technical_specification_pdf_url (){
    $terms = get_the_terms( $post->ID, 'wc-attibute-class' );
    if ( !empty($terms)) {
            $term = array_pop($terms);
                    $text= get_field('technical_specification_pdf', $term);
                    if (!empty($text)) {
                    echo $text;
                    }
    }
}


//  technical specification
add_action( 'woocommerce_single_product_summary', 'technical_specification_button', 30 );

function technical_specification_button() {
  global $product;
  echo '<a href=".'technical_specification_pdf_url()'.">technical specification</a>';
}

我需要在 Woocommerce 单页中显示小册子的按钮 link。 我的函数工作正常,只是值没有添加到 href 标签内。

在函数 technical_specification_pdf_url 中,您必须 return $text 才能将其附加到函数 technical_specification_pdf_url.

步骤 1:echo $text; 更新为 return $text;

第 2 步: 添加 return null; 函数 technical_specification_pdf_url 让您知道产品不存在 technical_specification_pdf_url 字段。

第三步:添加if ($url)检查是否存在条件,$url = technical_specification_pdf_url();。因为你在所有产品上都这样做,所以你应该忽略其他产品。

// add_action( 'woocommerce_single_product_summary', 'technical_specification_pdf_url', 32 );
function technical_specification_pdf_url (){
    $terms = get_the_terms( $post->ID, 'wc-attibute-class' );
    if ( !empty($terms)) {
            $term = array_pop($terms);
                    $text= get_field('technical_specification_pdf', $term);
                    if (!empty($text)) {
                       return $text;   // Fix here
                    }
    }

    // return null if doesn't exists technical_specification_pdf field
    return null;
}


//  technical specification
add_action( 'woocommerce_single_product_summary', 'technical_specification_button', 30 );

function technical_specification_button() {
  $url = technical_specification_pdf_url();
  // Add check URL here, if not exists, do nothing
  if ($url) {
    global $product;
    echo "<a href=". $url .">technical specification</a>";
  }
}