Я бы хотел добавить переменную, которая содержит функцию (будь то WordPress или пользовательская функция) в HTML, которая находится внутри другой переменной. Проблема в том, что когда я объединяю, он появляется вне контейнера div в передней части, где он должен находиться внутри контейнера.
Для примера, показанного здесь, я бы хотел, чтобы "$ stay_put" или PHP в целом генерировались в следующих элементах div:
function sort_sections( $sections ) {
$sections = explode(',', $sections);
$output = '';
/* $stay put needs to be able to hold any function */
$stay_put = wp_list_pages();
if ( empty( $sections ) ) {
return $output;
}
foreach( $sections as $section ) {
switch ( $section ) {
case 'section_a':
$output .= '<div>Section A</div>';
break;
case 'section_b':
$output .= '<div>Section B</div>';
break;
default:
break;
}
}
return $output;
}
То, что я придумал, но отображает переменную вне контейнера:
$stay_put
foreach( $sections as $section ) {
switch ( $section ) {
case 'section_a':
$output .= '<div>' . $stay_put . '</div>';
break;
case 'section_b':
$output .= '<div>' . $stay_put . '</div>';
break;
default:
break;
}
}
Если кто-то может помочь,
Заранее спасибо.
РЕДАКТИРОВАТЬ: Решение
function render_sections( $sections ) {
$sections = explode(',', $sections);
$output = '';
$stay_put = wp_list_pages(['echo' => false]);
if ( empty( $sections ) ) {
return $output;
}
foreach( $sections as $section ) {
switch ( $section ) {
case 'section_a':
$output .= '<div>Section A';
$output .= $stay_put;
$output .= '</div>';
break;
case 'section_b':
$output .= '<div>Section B';
$output .= $stay_put;
$output .= '</div>';
break;
default:
break;
}
}
return $output;
}