【问题标题】:Auto-formatting in Wordpress posts only仅在 Wordpress 帖子中自动格式化
【发布时间】:2017-01-10 04:53:36
【问题描述】:

我在我的主题中使用 remove_filter( 'the_content', 'wpautop' ); 来禁用 Wordpress 自动格式化,该自动格式化会在各处添加段落并经常弄乱布局。

但是,我的一位客户希望能够自己添加博客文章。他需要能够从所见即所得编辑器中格式化他的博客文章,至少要有段落。

是否可以仅在博客文章中,或在特定类型或属于特定类别的文章中允许自动格式化?

到目前为止,我发现的所有示例和文章都使用与上面列出的完全相同的代码。我还检查了https://codex.wordpress.org/Template_Tags,但没有看到任何可以帮助我解决这个问题的东西。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    既然您已经删除了常规的 wpautop 文件管理器,您可以在其位置添加自己的文件管理器,并在其中仅将 wpautop 应用于“post”类型的帖子:

    add_filter( 'the_content', 'smart_autop' );
    function smart_autop($content) {
        $post = get_post();
        if($post->post_type != 'post') return $content; // if not a post, leave $content untouched
        return wpautop($content);
    }
    

    希望这会有所帮助!

    【讨论】:

    • 工作就像一个魅力!
    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多