如何在wordpress中对the_content()和the_excerpt()设置字符限制 [英] How to set character limit on the_content() and the_excerpt() in wordpress

查看:16
本文介绍了如何在wordpress中对the_content()和the_excerpt()设置字符限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 wordpress 中对 the_content() 和 the_excerpt() 设置字符限制?我只找到了字数限制的解决方案 - 我希望能够设置输出的确切字符数.

How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit - I want to be able to set an exact amount characters of outputted.

推荐答案

您可以使用 Wordpress 过滤器回调函数.在您的主题目录中,找到或创建一个名为 functions.php 的文件并将以下内容添加到:

You could use a Wordpress filter callback function. In your theme's directory, locate or create a file called functions.php and add the following in:

<?php   
  add_filter("the_content", "plugin_myContentFilter");

  function plugin_myContentFilter($content)
  {
    // Take the existing content and return a subset of it
    return substr($content, 0, 300);
  }
?>

plugin_myContentFilter() 是您提供的函数,每次您通过 the_content() 请求帖子类型的内容(如帖子/页面)时,都会调用该函数.它为您提供内容作为输入,并将使用您从该函数返回的任何内容用于后续输出或其他过滤器函数.

The plugin_myContentFilter() is a function you provide that will be called each time you request the content of a post type like posts/pages via the_content(). It provides you with the content as an input, and will use whatever you return from the function for subsequent output or other filter functions.

您还可以将 add_filter() 用于其他函数,例如 the_excerpt() 以在请求摘录时提供回调函数.

You can also use add_filter() for other functions like the_excerpt() to provide a callback function whenever the excerpt is requested.

有关详细信息,请参阅 Wordpress 过滤器参考文档.

See the Wordpress filter reference docs for more details.

这篇关于如何在wordpress中对the_content()和the_excerpt()设置字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆