简体   繁体   English

在 php 中的 X 字符后隐藏?

[英]Hide after X characters in php?

I have a Wordpress site, and I want to set up a paywall for certain content.我有一个 Wordpress 网站,我想为某些内容设置付费专区。 I would like to show some intro of each such article for Google index and users as well, but rest of it would be hidden for guests and available for logged in (in my case paid users).我还想为 Google 索引和用户展示每篇此类文章的一些介绍,但其中的 rest 将对访客隐藏,可供登录(在我的情况下为付费用户)使用。

I successfully implemented is_user_logged_in() PHP statement from Wordpress, but I am not finding PHP code anywhere online about hiding everything after n-characters, or words.我从 Wordpress 成功实现is_user_logged_in() PHP 语句,但我没有在网上任何地方找到 PHP 代码关于隐藏 n 个字符或单词之后的所有内容。

My intended workflow is:我预期的工作流程是:

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

You can use WordPress default function wp_trim_words .您可以使用 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