2017-03-03 61 views
0

我的短码输出总是出现在我的自定义模板的顶部。短码输出总是显示在自定义模板的顶部

自定义模板

$tag= 'the_content'; 
remove_all_filters($tag); 
$postid = get_the_ID(); 
$post = get_post($postid); 
$content = do_shortcode($post->post_content); 

ob_start(); 
echo $content; 
$result = ob_get_contents(); 
ob_end_clean(); 
return $result; 

自定义简码

function signupform_shortcode($atts) { 
    extract(shortcode_atts(array(
     'socialmkt' => 'aweber' 
    ), $atts)); 

    if($socialmkt == 'aweber'){ 
     if($display == 'popup') { 
     return include_once('modal-aweber.php'); 
     } 

    } 
} 
add_shortcode('signupform', 'signupform_shortcode'); 

很html目标的中间位置短码。 我尝试添加ob_start(),我在其他帖子中阅读,但仍然无法正常工作。

+0

'模式,aweber.php'这是当前的HTML,我需要在HTML一个特定的地方进行打印。 –

回答

2

您的短代码回调将输出内容而不是返回它,这就是为什么你会看到它出现在页面的顶部。

使用输出缓冲(ob_start()/ob_get_contents())是解决问题的有效方法,但是您需要移动代码。

输出缓冲应发生在您的短代码回调中,其中需要返回值而不是输出。

function signupform_shortcode($atts) { 
    extract(shortcode_atts(array(
     'socialmkt' => 'aweber' 
    ), $atts)); 

    // Begin output buffering here. Any output below will be stored in a buffer. 
    ob_start(); 

    if ($socialmkt == 'aweber') { 
     if ($display == 'popup') { 

      // Return, as previously used, doesn't help in this context. 
      include_once('modal-aweber.php'); 
     } 

    } 

    // Return (and delete unlike ob_get_contents()) the content of the buffer. 
    return ob_get_clean(); 
} 
add_shortcode('signupform', 'signupform_shortcode'); 
+0

感谢您的回答!我试着哟说什么也没有。我从我的自定义模板中删除了(ob_start()/ ob_get_contents()),并添加到自定义简码中。除了该解决方案之外,我在两个文件中尝试了(ob_start()/ ob_get_contents())。 –

+0

完美的作品!这是缓存问题。 –

相关问题