如何为WordPress帖子设置最小字数

Do you run a blog with multiple authors? Then, you probably have wondered how you can set a minimum word count for your posts in WordPress. In this article, we will share with you a snippet that lets you set a minimum Word count for your WordPress posts. If a user tries to publish a post that is too small, then it will return an error telling them the post is not long enough.

您是否在博客中拥有多位作者? 然后,您可能想知道如何为WordPress中的帖子设置最小字数。 在本文中,我们将与您分享一个片段,该片段可让您为WordPress帖子设置最低字数。 如果用户尝试发布的帖子太小,则会返回一条错误消息,告诉他们该帖子不够长。

Simply open your theme’s functions.php file and paste the following code:

只需打开主题的functions.php文件并粘贴以下代码:

function minWord($content)
{
	global $post;
	$content = $post->post_content;
	if (str_word_count($content) < 100 ) //set this to the minimum number of words
	wp_die( __('Error: your post is below the minimum word count. It needs to be longer than 100 words.') );
}
add_action('publish_post', 'minWord');

You may change the minimum number of words from 100 to whatever you like. You can also customize the error to make it helpful.

您可以将最小字数从100更改为任意值。 您还可以自定义错误以使其有所帮助。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-set-a-minimum-word-count-for-wordpress-posts/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值