为WordPress文章添加转载申明版权信息等
浏览别人的博客文章时,会发现文章底部往往会有一些申明,如(本文转载于xxx的文章xxx)或(本文由xxx原创,禁止转载)之类的。如果这种申明完全靠写文章时手动添加,那会相当麻烦又累,特别想要修改时,那往期的文章那么多,可谓是欲哭无泪呀。
添加转载申明代码
找到主题中的functions.php文件,打开文件直接翻到最底部,在?>之前加入以下代码(根据自己的需要修改代码中的转载申明和版权信息):
// 添加原创和转载版权信息
function copyright($content) {
if(is_single()||is_feed()) {
$custom_fields = get_post_custom_keys($post_id);
if(!in_array('copyright_post_url',$custom_fields)) {
$content.='<div>本文为<a href="'.get_option('home').'" >'.'@'.get_bloginfo().'</a>原创文章,转载请注明出处:<a href="'.get_permalink().'" title="'.get_the_title().'">'.
get_permalink().'</a>,非常感谢!</div>';
} else {
$custom = get_post_custom($post_id );
$copyright_home = $custom['copyright_home'][0];
$copyright_home_url = $custom['copyright_home_url'][0];
$copyright_post = $custom['copyright_post'][0];
$copyright_post_url = $custom['copyright_post_url'][0];
if(empty($copyright_home)) $copyright_home = $copyright_home_url;
if(empty($copyright_post)) $copyright_post = $copyright_post_url;
$content.='<div>本文转载于';
if(!empty($copyright_home)) {
$content.='<a ';
if(!empty($copyright_home_url)) $content.= 'href="'.$copyright_home_url.'"';
$content.='>'.'@'.$copyright_home.'</a>的文章';
}
$content.=':<a href="'.$copyright_post_url.'" title="'.$copyright_post.'">'.
$copyright_post_url.'</a></div>';
}
}
return $content;
}
add_filter ('the_content', 'copyright');

当文章为原创时
当文章为原创时,你什么都不需要做,直接发布文章,上面的代码会自动为你添加版权申明。
当文章为转载时
当文章为转载时,你需要添加copyright_post_url自定义参数,以方便上面的代码识别为转载文章。copyright_post_url为转载文章的地址,copyright_post为转载文章的标题,copyright_home_url为转载文章作者的主页地址,copyright_home为转载文章的作者名,可以按需添加。
如何添加自定义参数?
在写文章页面中,找到右边的选项按钮,点击选择偏好设置,在弹出的页面中点击面板->额外,打开自定义字段即可。(每个WordPress版本自定义字段打开的方式可能不同,这个需要自己研究一下,一般不会太难找)。


此时在写文章页面的底部就会出来自定义字段的控制面板,第一次选择输入新栏目,然后输入名称和值,点击添加自定义字段即可。添加时会自动保存到现有列表里面,下次只需要在选择列表中选择需要的字段,然后输入值点击添加到自定义字段即可。


