我有几个问题
如何限制帖子中的字符?
如何确保每篇帖子都需要上传图像?
仅此而已,如果有人能帮忙,我将不胜感激。。
我有几个问题
如何限制帖子中的字符?
如何确保每篇帖子都需要上传图像?
仅此而已,如果有人能帮忙,我将不胜感激。。
对于第一个问题:可以在循环中使用摘录()而不是内容(),也可以使用以下函数:
function limitcontent($limit) {
$content = explode(\' \', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).\'...\';
} else {
$content = implode(" ",$content);
}
$content = preg_replace(\'/\\[.+\\]/\',\'\', $content);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
return $content;
}
用法:<?php echo excerpt(25); ?>
关于你的第二个问题:<罢工>你不能(好吧,这不是一种简单的方式,它将涉及挂接到pre\\u post\\u save,并在没有将图像附加到帖子的情况下重新记录保存-并且此功能不是不经检查就可以即时编写的功能-至少不是我)
但是,如果每个帖子都必须有一个图像,那么可以使用不同的方法,我已经使用过很多次了,那就是为没有附件的帖子定义默认图像。您可以按类别、按Taxonymy使用它,如果用户未定义任何post\\U缩略图,甚至可以定义默认的缩略图
编辑- you CAN - 经过5分钟的反思,以下是如何在帖子上强制上传图像:
add_action( \'pre_post_update\', \'krembo99_no_image_no_publish\' );
function krembo99_no_image_no_publish()
{
global $post;
$attachments = get_children( array( \'post_parent\' => $post->ID, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\') );
//$attachments = get_children( array(\'post_parent\' => $post->ID, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\') );
//if((!$attachments)) {
if(count($attachments) === 0){
wp_die( \'you can not publish a post without an image, please update attachments !\' );
}
}
可能有人想用警告栏更改wp\\U模具,但此功能现在可以使用。*-未完全测试,仅在帖子上。。。帖子应该有家长吗?如果是这样的话,一个帖子有家长意味着什么?此外,如果对有父母的帖子有一些限制,那么在哪里强制执行呢?在DB中我看不到。