繁体   English   中英

在 php 中的 X 字符后隐藏?

[英]Hide after X characters in php?

我有一个 Wordpress 网站,我想为某些内容设置付费专区。 我还想为 Google 索引和用户展示每篇此类文章的一些介绍,但其中的 rest 将对访客隐藏,可供登录(在我的情况下为付费用户)使用。

我从 Wordpress 成功实现is_user_logged_in() PHP 语句,但我没有在网上任何地方找到 PHP 代码关于隐藏 n 个字符或单词之后的所有内容。

我预期的工作流程是:

<?php
if ( is_user_logged_in() ) {
    the_content();
} else {
    echo 'show only 200 characters or words of the content code should be here';
}
?>

您可以使用 WordPress 默认 function wp_trim_words

<?php
    if ( is_user_logged_in() ) {
        the_content();
    } else {
        $content = get_the_content(); // $content is whatever your content field.
        echo wp_trim_words( $content, 200, ' ' );
    }
?>
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content();
//If your template has some other way of getting the post content then use that variable.
echo substr($content, 0, strpos($content, ' ', 200)); 
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM