【问题标题】:Overriding Excerpt in WordPress with Advanced Custom Field使用高级自定义字段覆盖 WordPress 中的摘录
【发布时间】:2016-01-06 17:04:43
【问题描述】:

我是 WordPress 新手,我们有一个表格供人们填写,以便将帖子提交到我们的网站。

我们使用高级自定义字段插件来填写我们需要的所有必要信息。我们的字段是 story_description。

我想让 story_description 使用所有摘录过滤器(长度等)并将其保存在 wp_posts 表中的 post_excerpt 下。

我将如何使用自定义长度和定义的摘录中的其他特定长度将其保存在该表中?我对所有过滤器和操作都是新手。

当帖子被保存时,有没有办法只覆盖 is 和 Treat 就像一个普通的摘录?

感谢任何回复。

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    您首先需要添加以下内容以允许在循环之外生成摘录(这在您的主题 functions.php 中):

    function rw_trim_excerpt( $text='' )
    {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        return wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    add_filter('wp_trim_excerpt', 'rw_trim_excerpt');
    

    这个函数来自jlengstorf answer

    然后,当您插入帖子时,请执行以下操作:

    wp_insert_post(array(
        // ... other stuffs ...
        post_excerpt => apply_filters('get_the_excerpt', $story_description)
    ));
    

    这将使用所有摘录 WP 函数过滤您的 $story_description 字符串。

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2015-01-21
      • 2017-05-01
      • 2018-09-02
      • 1970-01-01
      相关资源
      最近更新 更多