WordPress виджет: Включить отображается вне переноса виджета - PullRequest
0 голосов
/ 08 февраля 2019

Я создал виджет, который может включать любую часть страницы (.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

RenderedHTML

<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>

1 Ответ

0 голосов
/ 08 февраля 2019

Ладно, сам найду проблему.Это связано с функцией include, которая не может быть сохранена в переменной.Поэтому вам нужно создать для него отдельную функцию:

function:

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

variable:

$params[0]['after_widget'] = includeToVar(get_stylesheet_directory() . $fieldInput);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...