2013-02-22 54 views
2

試圖讓它起作用,甚至可以傳遞這樣的變量嗎?可能將變量傳遞給wordpress摘錄中的add_filter

function excerpt_read_more($output, $text) { 
    global $post; 
    return $output . '<a href="'. get_permalink($post->ID) . '" class="readmore">'.$text.'</a>'; 
} 
add_filter('the_excerpt', 'excerpt_read_more'); 

我想能夠做這樣的事

<?php the_excerpt('Read More...'); ?> 

因爲我希望它能夠在整個網站說不同的事情。例如,閱讀這篇文章,繼續閱讀帖子,查看這個配方。

回答

2

過濾器the_excerpt只有一個參數,所以你正在嘗試的是不可能的。

一種選擇是使用全局變量:

function excerpt_read_more($output) { 
    global $post, $my_read_more; 
    return $output 
     . '<a href="' 
     . get_permalink($post->ID) 
     . '" class="readmore">' 
     . $my_read_more 
     . '</a>'; 
} 
add_filter('the_excerpt', 'excerpt_read_more'); 

,然後調用的功能等:

<?php 
global $my_read_more; 
$my_read_more='read this post'; 
the_excerpt(); 
?> 

<?php 
global $my_read_more; 
$my_read_more='view this recipe'; 
the_excerpt(); 
?> 

另一個解決方案是使用自定義字段:How to customize read more link

爲了便於使用,創建了一個包含所有「閱讀更多」選項的Meta Box,因此在發佈後定義:Add a checkbox to post screen that adds a class to the title

+0

謝謝!這工作很好,沒有想到它將其作爲一個全球通過。感謝幫助。 – souporserious 2013-02-22 23:04:16

+0

另外,調用函數時不需要傳遞全局變量,因爲它已經在excerpt_read_more中定義。 – souporserious 2013-02-22 23:12:53