2014-03-26 29 views

回答

0

您可以使用此;

<?php 
function getFirstXWord($string, $a) { 
    $temp = explode(" ", $string); 
    return implode(" ", array_slice($temp, 0, $a, true)); 
} 

echo getFirstXWord("this is a content but do not trust this content", 5); 
?> 

這裏是一個工作演示:codepad

對於移動檢測部分,你可以使用php-mobile-detect類。有項目的例子,我沒有在這裏陳述它

0

立即想到兩種方法。

一種方法是使用PHP檢查客戶端的用戶代理,然後使用the_excerpt()獲取該帖子的摘錄。您可以使用下面的代碼來修改您找回的摘錄的長度。

add_filter('excerpt_length', function() { 
    return 20; 
}, 999); 

有關詳細信息上the_excerpt()click here;欲瞭解更多關於add_filterclick here的信息。

或者,您可以使用CSS和媒體查詢(更多信息here)來處理移動設備。基本上你需要持有您的文章內容兩個容器:

<div class="post desktop"> 
    Your post content here! 
</div> 

<div class="post mobile"> 
    Your mobile excerpt... 
</div> 

而在你的CSS:

.mobile { 
    display: none; 
} 

@media (max-width: < target device width goes here >) { 
    .desktop { 
     display: none; 
    } 

    .mobile { 
     display: block; 
    } 
} 
1

你可以在你的主題文件夾中添加一個functions.php文件,然後執行以下

add_filter("the_content", "plugin_myContentFilter"); 

function plugin_myContentFilter($content) 
{ 
    if (wp_is_mobile()) 
     return substr($content, 0, 100); // Show the first 100 characters of content if the user is visiting using a mobile device 
    else 
     return $content; 
} 

請注意,我沒有測試過這個,但理論上我相信它應該可以工作。

來源:

0

在你content.php您可以更改以下行:

<?php if (is_search()) : ?> 
<div class="entry-summary"> 
<?php the_excerpt(); ?> 
</div><!-- .entry-summary --> 

<?php if (is_search() || wp_is_mobile()) : ?> 
<div class="entry-summary"> 
<?php the_excerpt(); ?> 
</div><!-- .entry-summary --> 

您可以摘錄lenght設置爲你希望它是

相關問題