Bootstrap 3响应Wordpress自定义主题中的表代码替换


Bootstrap 3 responsive table code replacement in Wordpress custom theme

我正在处理bootstrap和wordpress集成。

在引导程序集成之前,已有<table>的帖子。

为了确保Bootstrap的组件,我想添加包装器和类来替换所有现有帖子中的现有表标记:

<table>...</table>

<div class="table-responsive">
    <table class="table table-condensed table-bordered">...</table>
</div>

有没有一个函数或过滤器可以在我的自定义主题的functions.php中实现全局代码注入?

一个可以在functions.php中使用的快速过滤器,不确定它是最好的方法,但会起作用:

    function wp_bootstrap_responsive_table( $content ) {  
        $content = str_replace( ['<table>', '</table>'], ['<div class="table-responsive"><table class="table table-bordered table-hover">', '</table></div>'], $content );
        return $content;  
    }  
    add_filter( 'the_content', 'wp_bootstrap_responsive_table' );