我试图在\\u内容之后挂接一些内容,但无论我如何挂接,我仍然会在\\u内容之前获取自定义内容:
add_filter( \'the_content\', \'ow_add_sctns_to_ctnt\' );
function ow_add_sctns_to_ctnt( $content ) {
$section = ow_create_section( \'section\' );
return \'<div>\' . $content . \'</div>\' . $section;
}
然后我仍然得到:
<p>My custom content</p>
<div>
<p>The Content</p>
</div>
查看代码,我希望自定义内容显示在内容之后。
ow\\U create\\U section是一个从高级自定义字段获取内容的函数。这是:
function ow_create_section( $name ) {
if ( have_rows( $name ) ):
while ( have_rows( $name ) ): the_row();
if ( get_row_layout() === \'section_accordions\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_accordions = new OwSctnAccordions( \'accns\', $args );
elseif ( get_row_layout() === \'section_content_single\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_content_single = new OwSctnContentSingle( \'ctnt-sgl\', $args );
elseif ( get_row_layout() === \'section_content_double\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_content_double = new OwSctnContentDouble( \'ctnt-dbl\', $args );
elseif ( get_row_layout() === \'section_callouts\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_callouts = new OwSctnCallouts( \'clts\', $args );
elseif ( get_row_layout() === \'section_gallery_image\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_gallery_image = new OwSctnGalleryImage( \'grid\', $args );
elseif ( get_row_layout() === \'section_gallery_media\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_gallery_media = new OwSctnGalleryMedia( \'grid\', $args );
elseif ( get_row_layout() === \'section_slider_image\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_slider_image = new OwSctnSliderImage( \'slider\', $args );
elseif ( get_row_layout() === \'section_slider_content\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_slider_content = new OwSctnSliderContent( \'slider\', $args );
elseif ( get_row_layout() === \'section_slider_slick\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_slider_slick = new OwSctnSliderSlick( \'slider\', $args );
elseif ( get_row_layout() === \'section_tabs\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_tabs = new OwSctnTabs( \'tabs\', $args );
elseif ( get_row_layout() === \'section_video\' ):
$args = array( \'content\' => get_sub_field( \'content\' ) );
$ow_sctn_video = new OwSctnVideo( \'video\', $args );
endif;
endwhile;
endif;
}
我错过了什么?提前感谢!
最合适的回答,由SO网友:freejack 整理而成
当内容挂钩被称为WordPress时,它已经在忙于生成页面HTML。\\u内容是一个过滤器挂钩,因此函数不能呈现任何内容,而应将其作为字符串返回。如果从钩子中调用函数,并且该函数呈现某些内容,那么它将在post内容之前呈现。
以下面的代码为例,在\\u内容中调用了两个函数。第一个函数呈现红色段落。这将显示在内容之前。第二个函数使用ob_start()
缓冲HTML内容,然后使用ob_get_clean()
. 这将按预期显示在内容之后。
function ow_create_section_1(){
echo \'<p style="width:100%;height:50px;background:red;">Section 1</p>\';
}
function ow_create_section_2(){
ob_start();
echo \'<p style="width:100%;height:50px;background:green;"></p>\';
return ob_get_clean();
}
add_filter( \'the_content\', \'ow_add_sctns_to_ctnt\' );
function ow_add_sctns_to_ctnt( $content ) {
$section_1 = ow_create_section_1();
$section_2 = ow_create_section_2();
return \'<div>\' . $content . \'</div>\' . $section_1 . $section_2;
}
这意味着
ow_create_section
函数是直接呈现一些内容,而不是使用缓冲区并返回它。确保内部使用的类
ow_create_section
正在返回内容,而不是直接呈现。
https://www.php.net/manual/en/function.ob-start.phphttps://www.php.net/manual/en/function.ob-get-clean.php