2009-05-20 80 views
0

我正在寫一個WordPress插件,過濾the_content,我想利用<!--more-->標籤的,但現在看來,它已被時間剝離出它到達我。這似乎不是一個過濾器,而是WordPress工作方式的一個功能。WordPress插件:找到<!--more-->在the_content

我當然可以從數據庫中重新加載已經加載的內容,但這聽起來像可能會導致其他麻煩。沒有<!--more-->刪除原始內容有沒有什麼好方法?

+0

看起來像你的回答在[WordPress.org] (http://wordpress.org/support/topic/271798) - 該解決方案是否適合您? – Michelle 2009-05-21 16:41:56

+0

我想這是一個選項 - $ post包含處理前的原始文章,如果我恢復到那個,那麼我可以同時使用和未修改的文章。但我擔心這可能會繞過其他處理? – 2009-05-22 13:45:14

回答

6

有機會,通過你的插件運行的時間,<!--more-->已轉換爲<span id="more-1"></span>

這是我在我的插件,其中<!--more-->標籤後立即注入一些標記使用:

add_filter('the_content', 'inject_content_filter', 999); 

function inject_content_filter($content) { 
    $myMarkup = "my markup here<br>"; 
    $content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'."\n\n". $myMarkup ."\n\n", $content); 
    return $content; 
} 
1

您可以使用以下代碼:

!is_single()將避免在View Post頁面中顯示更多鏈接。

add_filter('the_content', 'filter_post_content'); 
function filter_post_content($content,$post_id='') { 

     if ($post_id=='') { 
      global $post; 
      $post_id = $post->ID; 
     } 

     // Check for the "more" tags 
     $more_pos = strpos($filtered_content, '<!--more-->'); 
     if ($more_pos && !is_single()) { 
      $filtered_content = substr($filtered_content, 0, $more_pos); 

      $replace_by = '<a href="' . get_permalink($post_id) . '#more-' . $post_id 
        . '" class="more-link">Read More <span class="meta-nav">→</span></a>'; 

      $filtered_content = $filtered_content . $replace_by; 
     } 

     return $filtered_content; 
    } 
0

基於Frank Farmer's answer我解決了的single.php文件中生成多個標籤(<span id="more-...)這之後添加照片縮略圖:

// change more tag to post's thumbnail in single.php 
add_filter('the_content', function($content) 
{ 
    if(has_post_thumbnail()) 
    { 
     $post_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class'=>'img img-responsive img-thumbnail', 'style'=>'margin-top:5px;')); 
     $content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'.$post_thumbnail, $content); 
    } 
    return $content; 
}, 999);