简体   繁体   English

do_shortcode中的acf字段数据未填充

[英]acf field data in do_shortcode not populating

I am using the WordPress Advanced Custom Field plugin. 我正在使用WordPress高级自定义字段插件。 I have a custom built download button and shortcode. 我有一个自定义的下载按钮和简码。 Now I am using ACF fields to populate the download btn shortcode. 现在,我正在使用ACF字段来填充btn简码下载。 Everything seems to be working and download button is displaying as expected. 一切似乎都正常,并且下载按钮按预期显示。 The only problem is that ACF fields are not populating in the shortcode but its showing outside the shortcode code. 唯一的问题是,ACF字段不是在简码中填充,而是在简码代码之外显示。

<?php

  if (get_field('dn_btn_url1')) { 

    echo do_shortcode( '[download_btn url="'.get_field('dn_btn_url1').'" filename="'.get_field('dn_btn_txt1').'"]' );

  }

?>

The code without the if statement is working as expected but it doesn't hide the shortcode. 没有if语句的代码可以按预期工作,但不会隐藏简码。

The get_field function gets the post id from the global $post object so when it's passed into the shortcode function, it seems like it can't get the right post id. get_field函数从全局$post对象获取帖子ID,因此当将其传递给shortcode函数时,似乎无法获取正确的帖子ID。 Try putting your URL from the field into a variable so the URL is already passed through the function and into the shortcode function. 尝试将字段中的URL放入变量中,以便URL已通过函数传递到shortcode函数中。

Try this: 尝试这个:

<?php

  if (get_field('dn_btn_url1')) {

    $url = get_field('dn_btn_url1');

    echo do_shortcode( '[download_btn url="' . $url . '" filename="' . $url . '"]' );

  }

?>

You can do that by using sprintf function please try it once 您可以使用sprintf函数来做到这一点,请尝试一次

<?php 
  if(get_fiedl('dn_btn_url1')) {
  $url = get_field('dn_btn_url1');
  $filename = get_field('dn_btn_txt1');
  $shortcode = sprintf(
  '[download_btn url="'.$url.'" , 
  filename="'.$filename.'"]');
  echo do_shortcode( $shortcode );
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM