Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the easy-watermark domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the shortcodes-ultimate domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the limit-login-attempts-reloaded domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131
wordpress获取文章摘要excerpt时如何过滤掉短代码 - WP大海

在WordPress中,我们在获取文章SEO的description时,如果文章开头有短代码,那么可能会带上短代码的内容,这样很不友好。那么你希望过滤掉短代码,可以使用一些内置函数和自定义代码来实现。以下是一个方法,你可以通过添加自定义函数到你的主题的 functions.php 文件来实现这个功能。

步骤 1: 创建一个自定义函数来移除短代码

首先,创建一个自定义函数来移除短代码。你可以使用 strip_shortcodes 函数,这个函数会移除内容中的短代码,但不会移除短代码生成的HTML(如果有的话)。

function remove_shortcodes_from_content($content) { 
    return strip_shortcodes($content); 
}

步骤 2: 修改获取摘要的函数

WordPress 有一个内置函数 wp_trim_excerpt,用于生成文章的摘要。为了在使用这个函数时移除短代码,你可以创建一个自定义的摘要函数。

function custom_wp_trim_excerpt($text) {
  $raw_excerpt = $text;
  if ( '' == $text ) {
    $text = get_the_content('');
    $text = strip_shortcodes($text); // 移除短代码
    $text = apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');

    if (mb_strlen($text) > 55) {
        if (false !== strpos($text, '<!--more-->')) {
            $text = explode('<!--more-->', $text, 2)[0];
        } else {
            $text = mb_substr($text, 0, 55) . $excerpt_more;
        }
    }
  }
  return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('wp_trim_excerpt', 'wp_trim_excerpt');
add_filter('wp_trim_excerpt', 'custom_wp_trim_excerpt', 10, 1);

这个函数首先检查是否已经存在内容摘要,如果没有,它会获取文章内容并移除短代码。然后,它使用标准的WordPress摘要逻辑来生成摘要。

步骤 3: 使用自定义摘要函数

现在,每当你在模板文件中调用 the_excerpt() 时,它都会使用你自定义的摘要函数,从而移除短代码。

例如,在你的主题模板文件中,你可以这样调用摘要:the_excerpt();

注意事项

  • 上述方法会全局修改摘要生成逻辑。如果你只想在特定地方移除短代码,你可能需要更精细地控制何时应用这个自定义函数。
  • 如果你使用的是自定义字段或者高级自定义字段(ACF)来存储摘要,你可能需要在获取这些字段内容时也应用 strip_shortcodes 函数。

 

常见问题
如何下载?
可以点击右侧边栏或者文章底部的【立即下载】按钮。然后按照【网盘地址】进行下载。
不是最新版本?
可以提交工单。或者发邮件到 uishop@qq.com。
声明:1、本站大部分资源均为网络采集所得,仅供用来学习研究,请于下载后的24h内自行删除,正式商用请购买正版。2、所有汉化类文件和个别标注了“原创”的产品均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。3、如若本站内容侵犯了原著者的合法权益,请携带相关版权文件联系我们进行下架或删除。4、虚拟下载类资源具有可复制性,一经下载后本站有权拒绝退款或更换其他商品!
0

评论0

没有账号?注册  忘记密码?

社交账号快速登录