2012-04-24 84 views
0

我正在尋找將rel =「image_src」添加到所有出現在wordpress中的帖子上的圖片標籤。我試圖從編輯/wp-includes/media.php:將代碼添加到所有<img>關於wordpress的標籤

$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />'; 

$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" rel="image_src" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />'; 

,但無濟於事。我是否在正確的位置,還是有其他我應該編輯的東西?

由於大部分

+2

我可能會使用JQuery而不是試圖將其破解到服務器端代碼。 – dqhendricks 2012-04-24 19:04:45

+1

我強烈建議您不要編輯'/ wp-includes/media.php'或任何核心wordpress文件。您下次更新wordpress時可能會覆蓋您的更改。僅在主題或自定義插件中進行更改。有'actions'和'filters'可以用來影響wordpress核心的部分。 – Robbie 2012-04-24 19:06:37

回答

1

我會去jquery,而不是修改WordPress的核心文件。我可能會使用下面的js:

<script> 
jQuery(document).ready(function ($) { 
    $("img").attr("rel","image_src"); 
}); 
</script> 
2

這可能是很不好做這種方式,因爲HTML正則表達式時一般不贊成,但它是跳躍到想到的第一件事。未經測試,但它應該讓你開始。

add_filter('the_content', 'add_img_src', 20); 
function add_img_src($content) 
{ 
    preg_match_all('/<img(.*?)>/', get_the_content(), $matches); 
    if(count($matches[1]) && is_single()) 
    { 
     foreach($matches[1] as $count => $match) 
     { 
      str_replace($match, $match.' rel="image_src"', $content); 
     } 
    } 
    return $content; 
} 
+0

這個功能屬於哪裏?忽略原始? – bswinnerton 2012-04-24 19:13:47

+0

你不想編輯核心文件。把它放在主題文件夾的functions.php文件中。如果沒有,請創建一個。 – maiorano84 2012-04-24 19:15:28

+0

如果這有幫助,請標記爲「已回答」。 – maiorano84 2012-04-24 20:15:51

相關問題