2013-08-28 48 views
1

在我的WordPress博客中,我使用了很多摘錄。在我的主頁上,我想在文本(沒有CSS)的同一行上添加「閱讀更多...」,但是我不希望在我的新聞頁面上顯示這樣的內容。我發現這個代碼正是我想要的,但它適用於所有的摘錄。我如何才能在主頁上執行此操作?節選「閱讀更多...」嵌入文字

function new_excerpt_more($more) { 
    return ' <a class="read-more" href="' 
     . get_permalink(get_the_ID()) 
     . '">Read More...</a>'; 
} 
add_filter('excerpt_more', 'new_excerpt_more'); 
+0

我相信這是你在找什麼:http://stackoverflow.com/問題/ 4082662/multiple-excerpt-length-in-wordpress – mikowl

+0

@MichaelElias我不是在尋找不同的長度,而是我想要在我的主頁上閱讀更多內容,但不是在我的新聞頁面上 – BDGapps

回答

2

這應該工作:

function new_excerpt_more($more) { 
    if(is_home()) 
    $more = ' <a class="read-more" href="'. get_permalink(get_the_ID()) . '">Read More...</a>'; 

    return $more; 
} 
add_filter('excerpt_more', 'new_excerpt_more'); 

編輯:在你的主題,在循環之前,您應該添加一個變量(標誌),看它是否是你想要的網頁:

$my_flag = is_page(array(id, id, id, id, 'page-name', 'page-slug'); 

if(have_posts()) : 
    while(have_posts()) : 
    // Your code 
    endwhile; 
endif; 

而在你的函數(new_excerpt_more),你必須檢查它像這樣:

function new_excerpt_more($more) { 
     global $my_flag; 

     if(is_home() || $my_flag) 
     $more = ' <a class="read-more" href="'. get_permalink(get_the_ID()) . '">Read More...</a>'; 

     return $more; 
    } 
    add_filter('excerpt_more', 'new_excerpt_more'); 

我還沒有測試過它,但它應該可以工作。但是,不應該假設頁面顯示單個內容?它絕不會只顯示摘要(當然,這將取決於你的主題:P)

文檔: is_home()

+0

如果是新聞頁面和關於頁面。這是否只適用於家庭 – BDGapps

+0

@ BDGapps如果你想在其他網頁上,那麼你可以使用is_page('page_slug')。對於特定類別,您可以使用is_category('category_slug')。請參閱 is_page(http://codex.wordpress.org/Function_Reference/is_page)和 is_category(http://codex.wordpress.org/Function_Reference/is_category)文檔。 – Skaparate

+0

如何使用is_page如果我只有ID – BDGapps