strip_shortcodes不起作用

时间:2017-05-30 07:42:53

标签: php wordpress function

我是Wordpress的初学者,我正在尝试从数据库中删除帖子内容中的所有短代码。我发现为wordpress创建的函数名为“strip_shortcodes()”,但我真的不知道如何使用它。我在网上看了,我仍然不知道我的错误在哪里......

有代码:

$all_pages = $wpdb->get_results (
    "SELECT * FROM " . $post_table . " WHERE post_type = 'page' "
);

foreach ($all_pages as $page) {
    echo '<h1>' . $page->post_title . '</h1>';
    $content = $page->post_content;
    echo strip_shortcodes($content) . PHP_EOL;
}

结果:

  

[spacer size =“5”] [tabs style =“1”] [tab title =“Définition”] [标题   border =“#cf3549”color =“#444444”style =“1”] Qu'est-cequ'unhébergement   网络? [/ heading] Il s'agit en fait de la location mensuelle(ou annuelle)   d'un serveur,en d'autres termes la location d'une machine ressemblant   vaguementàunPCdépouillédesonécranetsanssystème   d开发传统纪念Windows ou Mac。 Lessystèmes   开发服务sügénéralementdeslogiciels libres   comme Linux,nginx ou des logiciels payants comme Windows server。乐   serveur web n'est que lapartiematérielle(hardware),qu'il faut   coupàunsystèmed'-exploitationintontàsessompétences,   c'est la partie logicielle(软件)。

例外结果:

  

Qu'est-cequ'unhébergementweb? Il s'agit en fait de la location   menuelle(ou annuelle)d'un serveur,en d'autres termes la location   d'une machine ressemblantvaguementàunPCdépouillédesonécranet   sanssystèmed'asseneltraditionnel comme Windows ou Mac。莱斯   systèmesd'etilddes serveurssontgénéralementdeslogiciels   libres comme Linux,nginx ou des logiciels payants comme Windows   服务器。 Le serveur web n'est que lapartiematérielle(硬件),   qu'il fautcoupleràunysystèmed'expractantantàses   compétences,c'est la partie logicielle(software)。

有人可以告诉我有什么问题吗?

3 个答案:

答案 0 :(得分:4)

            $content = preg_replace('#\[[^\]]+\]#', '',$page->post_content);
            echo apply_filters('the_content', $content);

试试这个

答案 1 :(得分:2)

查看帖子“主页”页面时,请删除所有短代码,但在其他页面上,例如single.php,请不要删除短代码。

function remove_shortcode_from_index( $content ) {
  if ( is_home() ) {
    $content = strip_shortcodes( $content );
  }
  return $content;
}
add_filter( 'the_content', 'remove_shortcode_from_index' );

点击此处了解详情:https://codex.wordpress.org/Function_Reference/strip_shortcodeshttps://developer.wordpress.org/reference/functions/strip_shortcodes/

试试这个remove_shortcode()函数可能会有所帮助。

Wordpress remove shortcode from content

答案 2 :(得分:0)

您的代码看起来很不错,但您缺少将其保存在数据库中。回声的输出是什么? 我会这样做(未经测试):


    $all_pages = $wpdb->get_results (
        "SELECT * FROM " . $post_table . " WHERE post_type = 'page' "
    );

    foreach ($all_pages as $key => $page) {
        $all_pages[$key]->post_content = strip_shortcodes($page->post_content);
        // Save the content to the database
        wp_update_post($all_pages[$key]);
    }

相关问题