用高级自定义字段覆盖WordPress中的摘录


Overriding Excerpt in WordPress with Advanced Custom Field

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

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

我想让这个故事描述使用所有的摘录过滤器(长度等),并将其保存在wp_posts表的post_except下。

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

有没有一种方法可以在保存帖子时像普通摘录一样覆盖Is和treat?

我感谢您的回复。

您首先需要添加以下内容,以允许在循环之外生成摘录(这在您的主题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答案。

然后,当你插入你的帖子做如下:

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

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