2017-11-04 102 views
0

我试图在短代码函数中使用do_shortcode。问题是,当我在我的页面上使用[terms_and_conditions]时,它不解析do_shortcode,我看到[MM_Form_Field type='custom' id='1' isRequired='true' class='']。它没有被执行。do_shortcode不解析里面的短代码

这是我的函数看起来像:

function terms_and_conditions($atts) { 
    // Variables 
    $terms = get_field('terms_content', 'options'); 
    $checkbox = get_field('terms_checkbox_msg', 'options'); 
    // $smarttag = get_field('terms_smarttag', 'options'); 
    $smarttag = do_shortcode(get_field('terms_smarttag', 'options')); 

    var_dump($smarttag); 

    $html_out = ''; 

    $html_out .= '<section id="terms">'; 

     $html_out .= '<div class="terms-content-wrapper">'; 
      $html_out .= $terms; 
     $html_out .= '</div>'; // end terms-content-wrapper 

     $html_out .= '<div class="terms-checkbox-wrapper">'; 
      $html_out .= '<div class="terms-checkbox">'; 
       $html_out .= do_shortcode($smarttag); 
       $html_out .= '<p>' . $checkbox . '</p>'; 
      $html_out .= '</div>'; // end terms-checkbox 
     $html_out .= '</div>'; // end terms-content-wrapper 

    $html_out .= '</section>'; // end tip-wrapper 

    return $html_out; 
} 
add_shortcode('terms_and_conditions', 'terms_and_conditions'); 
+0

get_field('terms_smarttag','options')'准确返回什么?另外,你在做一些奇怪的短代码魔术或者在do_shortcode的返回值上运行do_shortcode是一个错误吗? – janh

+0

@janh有人告诉我,在这种情况下,我需要运行'do_shortcode'两次。我认为这也是不正确的,我把它命名为'$ html_out。= $ smarttag;'。 'get_field'返回[MM_Form_Field type ='custom'id ='1'isRequired ='true'class ='']。 –

+0

get_field('terms_smarttag','options')'的内容是什么?我不认为你需要两次运行do-shortcode。可能要考虑嵌套它们:''[shortcode_a] [shortcode_b] [/ shortcode_a]' – admcfajn

回答

0

使用ob_start可以彻底清理你的简码。但你可能更喜欢避开它们。这应该工作...

<?php 
function terms_and_conditions($atts) { 
    // Variables 
    $terms = get_field('terms_content', 'options'); 
    $checkbox = get_field('terms_checkbox_msg', 'options'); 
    $smarttag = get_field('terms_smarttag', 'options'); 

    ob_start(); 
    ?> 
    <section id="terms"> 
     <div class="terms-content-wrapper"> 
     <?php 
     if($terms){ 
      echo $terms; // <- is terms an array or a string? 
     } 
     ?> 
     </div> 
     <div class="terms-checkbox-wrapper"> 
      <div class="terms-checkbox"> 
       <?php 
       echo do_shortcode($smarttag); 
       if(!empty($checkbox)){ 
        echo '<p>' . $checkbox . '</p>'; // <- is checkbox an array or a string? 
       } 
       ?> 
      </div> 
     </div> 
    </section> 
    <?php 
    return ob_get_clean(); 
} 
add_shortcode('terms_and_conditions', 'terms_and_conditions'); 

你可能想尝试在改变你在你的简码使用引号另一件事:

例如,使用此:

[MM_Form_Field type="custom" id="1" isRequired="true" class=""] 

,而不是这样的:

[MM_Form_Field type='custom' id='1' isRequired='true' class='']