2014-06-10 76 views
1

我使用WordPress中下面的PHP應用不同類別的職位我的首頁上取決於他們的位置WordPress的摘錄長度改變這些職位的摘錄長度:變化取決於後級

function twentytwelvechild_custom_excerpt_length($length) { 
    global $wp_query; 
    if($wp_query->current_post%5 == 0) return 30; 
    else return 12; 
} 
add_filter('excerpt_length', 'twentytwelvechild_custom_excerpt_length', 12);` 

這給了每5 30摘錄長度,其餘12的第一篇文章,但我不想做這種方式。

如何使用PHP將課程的摘錄長度更改爲30個單詞,將半個單詞改爲12個單詞?

希望這是有道理

詹姆斯

+0

你是什麼意思的PHP?什麼是期望的輸出?請確切地說。你的問題沒有意義 – Trix

回答

1

選項1:

使用摘錄長度過濾器,然後使用wp_trim_words()剝離下來的一些在網頁上讓他們都30。

選項2:

從原始過濾器鉤取出條件,並返回30.創建一個新的函數將返回12,但實際上並沒有把它撈起來。

function twentytwelvechild_custom_excerpt_length($length) { 
    return 30; 
} 
add_filter('excerpt_length', 'twentytwelvechild_custom_excerpt_length', 12); 

function twentytwelvechild_custom_excerpt_short($length) { 
    return 12; 
} 

然後在頁面上使用以下內容時,您想顯示較短的摘錄。

add_filter('excerpt_length', 'twentytwelvechild_custom_excerpt_short', 20); 
the_excerpt(); 
remove_filter('excerpt_length', 'twentytwelvechild_custom_excerpt_short', 20); 

通過你改變長度摘錄你輸出之前添加過濾器12.你正在展示然後取出過濾網,讓任何下列職位將回到正常的30

+0

謝謝你很快回到我身邊Nathan。如何在類'.half'的帖子上使用wp_trim_words()? –

+0

我已經解決了!基本上我上面使用的PHP對每一個帖子頁面都有影響 - 檔案,博客頁面,搜索結果。我問了這個問題,因爲我在設計我的搜索頁面,並希望搜索結果看起來不同於歸檔和主頁佈局。所以我只是做了上面的條件'更有條件'的函數twentytwildchild_custom_excerpt_length($ length){ \t global $ wp_query; \t if($ wp_query-> current_post%5 == 0 &&(is_home()|| is_archive()))return 30; \t否則返回12; } add_filter('excerpt_length','twentytwelvechild_custom_excerpt_length',12);' –

+0

@JamesWaterhouse您應該添加您的評論作爲答案 –

1
摘錄

我已經解決了!基本上我上面使用的PHP對每一個帖子頁面都有影響 - 檔案,博客頁面,搜索結果。我問了這個問題,因爲我在設計我的搜索頁面,並希望搜索結果看起來不同於歸檔和主頁佈局。所以我只是提出了上述條件'更有條件'

function twentytwelvechild_custom_excerpt_length($length) { global $wp_query; if($wp_query->current_post%5 == 0 && (is_home() || is_archive())) return 30; else return 12; } add_filter('excerpt_length', 'twentytwelvechild_custom_excerpt_length', 12);