自定义快捷代码不会显示包装的内容

时间:2019-09-28 作者:userone2

我正在尝试创建一个插件,为我的自定义bootstrap 4布局提供一些短代码实用程序。我正在读shortcode APIdocs 但我无法显示包装在我创建的短代码中的内容。插件类加载正确,但如果我加载我制作的测试页面,它将只显示页眉和页脚,短代码内的内容将不会加载。这有解决办法吗?代码有什么问题?

<?php

/*
* Plugin Name: BootstrapGrid
*/


class BootstrapUtilsGrid {

  public function __construct()
  {
    add_shortcode(\'page_container\', array( $this, \'pageContainer\' ) );
    add_shortcode(\'page_col\', array( $this, \'pageCol\' ) );
  }

  public function pageCover()
  {
// code here
  }

  public function parallaxSection()
  {
// code here
  }

  public function pageContainer($atts, $content)
  {
    $output = \'\';
    $val = shortcode_atts(array(
        \'type\' => \'container\'
    ),$atts);

    switch($val){
      case \'container\':
        echo \'<div class="container page-content"><div class="row">\'.do_shortcode($content).\'</div></div>\';
        break;
      case \'container-fluid\':
        echo \'<div class="container-fluid page-fluid-content"><div class="row">\'.do_shortcode($content).\'</div></div>\';
        break;
    }
  }

  public function pageCol($atts, $content)
  {
    $output = \'\';
    $val = shortcode_atts(array(
        \'type\' => \'col-12\',
        \'mobile\' => \'display\'
    ),$atts);

    if( $val[\'mobile\'] === \'hide\' ){
      switch($val[\'type\']){
        case \'col-3\':
          echo \'<div class="col-md-3 col-lg-3 d-none d-sm-none d-md-block">\'.$content.\'</div>\';
          break;
        case \'col-4\':
          echo \'<div class="col-md-4 col-lg-4 d-none d-sm-none d-md-block">\'.$content.\'</div>\';
          break;
        case \'col-6\':
          echo \'<div class="col-md-6 col-lg-6 d-none d-sm-none d-md-block">\'.$content.\'</div>\';
          break;
        case \'col-8\':
          echo \'<div class="col-md-8 col-lg-8 d-none d-sm-none d-md-block">\'.$content.\'</div>\';
          break;
        case \'col-12\':
          echo \'<div class="col-md-12 col-lg-12 d-none d-sm-none d-md-block">\'.$content.\'</div>\';
          break;
      }
    }
    else{
      switch($val[\'type\']){
        case \'col-3\':
          echo \'<div class="col-sm-12 col-md-3 col-lg-3">\'.$content.\'</div>\';
          break;
        case \'col-4\':
          echo \'<div class="col-sm-12 col-md-4 col-lg-4">\'.$content.\'</div>\';
          break;
        case \'col-6\':
          echo \'<div class="col-sm-12 col-md-6 col-lg-6">\'.$content.\'</div>\';
          break;
        case \'col-8\':
          echo \'<div class="col-sm-12 col-md-8 col-lg-8">\'.$content.\'</div>\';
          break;
        case \'col-12\':
          echo \'<div class="col-sm-12 col-md-12 col-lg-12">\'.$content.\'</div>\';
          break;
      }
    }
  }


}

$init = new BootstrapUtilsGrid;

?>

1 个回复
最合适的回答,由SO网友:ZecKa 整理而成

因为短代码必须返回字符串。避免在短代码中使用“echo”函数。

而不是这样做:

function my_shortcode_function($content){

  echo \'<div class="wrap">\';
  echo \'<div class="col">\'.$content.\'</div>\';
  echo \'</div>\';

}
这样做:

function my_shortcode_function($content){

  $shortcode_return = \'<div class="wrap">\';
  $shortcode_return .= \'<div class="col">\'.$content.\'</div>\';
  $shortcode_return .= \'</div>\';

  return $shortcode_return;

}
或使用对象缓冲:

function my_shortcode_function($content){

  ob_start(); ?>

  <div class="wrap">
    <div class="col"><?php echo $content; ?> </div>
  </div>

  <?php
  $shortcode_return = ob_get_clean();

  return $shortcode_return;

}