【问题标题】:Wordpress widget: Include is rendered outside widget wrapWordpress 小部件:包含在小部件包装之外呈现
【发布时间】:2019-02-08 10:33:08
【问题描述】:

我制作了一个小部件,它可以在小部件区域内包含任何页面部分(.php 文件)。 问题是包含文件内的所有内容都呈现在小部件包装器之外(小部件之前/之后):

php

function include_php_acf_fields( $params ) {

// get widget vars
$widget_name = $params[0]['widget_name'];
$widget_id = $params[0]['widget_id'];

// bail early if this widget is not a Text widget
if( $widget_name != 'Include Theme PHP-file' ) {

    return $params;

}

// add image to after_widget

$fieldInput = get_field('input-file-name','widget_' . $widget_id);
$fileLocation = get_field('file-location','widget_' . $widget_id);
$trimSlash = ltrim($fieldInput, '/');
$replaceDash = str_replace('/', '_', $trimSlash);
$widgetID = str_replace('.php', '', $replaceDash);

$params[0]['before_widget'] = '<div id="' . $widgetID . '" class="widget cf test">';

if ( $fieldInput ) {    
    if(!is_admin()) {   
        if($fileLocation == 'parent-theme') {
            include(get_template_directory() . $fieldInput);
        } elseif($fileLocation == 'child-theme') {
            $params[0]['after_widget']  = include(get_stylesheet_directory() . $fieldInput);
        }
    }
}
$params[0]['after_widget'] .= '</div>'; 
// return
return $params;

}

// return

渲染的 HTML

<div class="posts-wrap cf content-post-gallery">
   <!-- some content -->
</div>
<div id="post-parts_part-archive-artists" class="widget cf test">
  <!-- .post-wrap with content should go here --->
</div>

【问题讨论】:

    标签: php html include widget


    【解决方案1】:

    好的,我自己解决问题。它与不能存储在变量中的include 函数有关。所以你需要为它创建一个单独的函数:

    功能:

    function includeToVar($file) {
            ob_start();
            include($file);
            return ob_get_clean();
        }
    

    变量:

    $params[0]['after_widget'] = includeToVar(get_stylesheet_directory() . $fieldInput);
    

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多