簡體   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