短代码输出始终显示在自定义模板的顶部 [英] Shortcode output always showing at top of custom template

查看:29
本文介绍了短代码输出始终显示在自定义模板的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的短代码输出总是出现在我的自定义模板的顶部.

my shortcode output always appear on the top of my custom template.

自定义模板

$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() 但仍然无法正常工作.

The shortcode it's place in the middle of a html landing. I try adding ob_start() that i read in other posts but still not working.

推荐答案

您的简码回调将输出内容而不是返回内容,这就是您看到它出现在页面顶部的原因.

Your shortcode callback is going to output content rather than return it which is why you're seeing it appear at the top of your page.

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

Using output buffering (ob_start() / ob_get_contents()) is a valid way of addressing the problem however you need to move the code.

输出缓冲应该发生在你的短代码回调中,这里需要返回值而不是输出.

Output buffering should be taking place inside your shortcode callback where the return value is required instead of output.

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' );

这篇关于短代码输出始终显示在自定义模板的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆