2017-04-27 26 views
0

我有一個代碼,可以讓頁面內容:得到WordPress的網頁內容,並限制詞

<?php 
$page = get_page_by_title('page-name'); 
$content = apply_filters('the_content', $page->post_content); 
echo $content; 
?> 

這是一個問題,工作代碼,我不知道我怎麼能限制詞。我只想顯示該頁面的20個字。

回答

1

string_limit_words()既不是一個標準的PHP或WordPress的功能,因此該解決方案ISN」廣泛適用。任何人希望以所描述的方式限制文本長度,都必須編寫自己的string_limit_words()函數或追蹤您使用的相同代碼。

我的建議是使用wp_trim_words()這是一個內置於WordPress的功能,用於此確切目的。

https://codex.wordpress.org/Function_Reference/wp_trim_words

實施例:

<?php 
$page = get_page_by_title('page-name'); 
$trimmed_content = wp_trim_words($page->post_content, 20, ''); 
echo apply_filters('the_content', $trimmed_content); 
0

對於那些誰與同患難遇到這是我如何獲得工作的..

<?php 
$page = get_page_by_title('about us'); 
$content = apply_filters('the_content', $page->post_content); 
echo string_limit_words($content,20); 
?> 

希望這有助於感謝的

相關問題