2017-11-04 100 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='']