【问题标题】:Wordpress Shortcode Function displays included file above all contentWordpress 简码功能在所有内容上方显示包含的文件
【发布时间】:2018-09-14 06:59:02
【问题描述】:

这是我的功能:

            function include_product_table( $atts ) {
            $a = shortcode_atts( array(
            'tablename' => ''
            ), $atts );
            $tablefile = '/home/panacol1/data/wp-content/themes/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php';

            return include $tablefile;

            }
            add_shortcode('producttable', 'include_product_table');

我的帖子包含带有一些 NextGen 图像的文本内容,然后以:

'[producttable tablename="bonding-plastics-and-dissimilar-substrates"]'

调用包含表格 HTML 的文件。但表格显示在内容之前。它也显示在 Wordpress 仪表板中帖子编辑页面的标题上方。

如果我从 'return include $tablefile;' 行中删除 'include'让它'return $tablefile;'文件的路径显示在我希望表格显示的内容底部。'

感谢任何帮助。

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    您可以通过多种方式做到这一点。我不知道你的 tablename.php 文件是什么样的,但我假设它基本上是纯 HTML。

    function include_product_table( $atts ) {
        $a = shortcode_atts( array(
          'tablename' => ''
        ), $atts );
        $tablefile = ABSPATH . '/wp-content/themes/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php'; // filepath relative to WP install path.
        if (!is_file($tablefile)) { 
              // Bail out if the file doesn't exist
              return false;
        }
    
        ob_start();
        include $tablefile;
        $table_data = ob_get_contents();
        ob_end_clean();
    
        return $table_data;
    }
    add_shortcode('producttable', 'include_product_table');
    

    当您调用 ob_get_contents(); 时,PHP 输出缓冲区将捕获 tablename.php 想要显示的任何 HTML 并将其放入 $table_data 中。

    运行 ob_end_clean() 只是在清理自己。

    【讨论】:

      【解决方案2】:

      如果你在php文件中绘制表格,我建议你将表格信息放入数组中:

        function include_product_table( $atts ) {
              $a = shortcode_atts( array(
              'tablename' => ''
              ), $atts );
              $tablefile = '/home/panacol1/data/wp-content/themes/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php';
      
              require_once($tablefile);
      
              table_information = function_in_your_php_file_return_table_information();
      
              return table_information;
      
              }
              add_shortcode('producttable', 'include_product_table');
      

      你可以在这个函数中绘制它然后返回它或返回信息然后绘制它。

      【讨论】:

      • 这行得通。我所做的是将整个表放在一个变量 '$table='...
        '; '那么,在我的函数中,require_once($tablefile);'和 'return $table;'' 谢谢!
      【解决方案3】:

      你不应该返回,在 php.ini 中包含函数。 例如你的表格文件(pa-1.php):

      <?php
      function p_1(){
      return "Hi";
      }
      ?>
      

      还有你的简码:

      function include_product_table( $atts ) {
                  $a = shortcode_atts( array(
                  'tablename' => ''
                  ), $atts );
                  $tablefile = get_template_directory().'/Divi-child/producttables/pt-'.$a[ 'tablename' ].'.php';
      
       include $tablefile;
      $function_name = "p_".$a[ 'tablename' ];
      return $function_name();
      
                  }
                  add_shortcode('producttable', 'include_product_table');
      

      【讨论】:

        猜你喜欢
        • 2018-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多