如何将代码挂钩以显示在_内容之后?

时间:2019-09-12 作者:drjorgepolanco

我试图在\\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;
}
我错过了什么?提前感谢!

1 个回复
最合适的回答,由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

相关推荐

将PHP 7.1升级到7.3后调用未定义的函数MySQL_Connect()

我在Debian/Raspbian buster服务器上托管自己的WP站点。我将PHP 7.1升级到7.3,网站崩溃了。我不明白为什么它会坏,因为mysql\\u connect()不推荐用于7。x(虽然wp-db.php似乎广泛使用它,而wp一直在提醒您升级到7.3)。因此,我希望在全新的7.3安装php中启用各种模块。ini。MySQLi 被注释掉了,所以我启用了它(和其他一些),但仍然得到相同的错误。Call to undefined function mysql_connect()... wp-d