无插件实现 WordPress 文章尾部版权信息模块的三种方法

通过代码自动调用 WordPress 网站作者信息,实现在文章尾部显示文章作者信息。

删除了《WordPress文章版权插件:Add Post URL》后总是感觉少了点东西,可能是因为看习惯在文章下面有个版权信息。从网上搜了一下无插件实现 WordPress 文章尾部版权信息模块的文章还挺多,测试了几个感觉还不错,分享给俍友们。

方法一:代码自动调用网站作者信息,不但修改起来比较方便,如果是多作者博客,可以根据不同作者文章显示不一样的版权信息。

20190418_01.png

步骤一、将下面的代码直接添加到当前主题的 functions.php 文件的最后位置。

//作者信息
function wpb_author_info_box( $content ) {
global $post;
// Detect if it is a single post with a post author
if ( is_single() && isset( $post->post_author ) ) {
// Get author's display name
$display_name = get_the_author_meta( 'display_name', $post->post_author );
// If display name is not available then use nickname as display name
if ( empty( $display_name ) )
$display_name = get_the_author_meta( 'nickname', $post->post_author );
// Get author's biographical information or description
$user_description = get_the_author_meta( 'user_description', $post->post_author );
// Get author's website URL
$user_website = get_the_author_meta('url', $post->post_author);
// Get link to the author archive page
$user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
if ( ! empty( $display_name ) )
$author_details = '<p class="author_name">About ' . $display_name . '</p>';
if ( ! empty( $user_description ) )
// Author avatar and bio
$author_details .= '<p class="author_details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</p>';
$author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';
// Check if author has a website in their profile
if ( ! empty( $user_website ) ) {
// Display author website link
$author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>';
} else {
// if there is no author website then just close the paragraph
$author_details .= '</p>';
}
// Pass all this info to post content
$content = $content . '<footer class="author_bio_section" >' . $author_details . '</footer>';
}
return $content;
}
// Add our function to the post content filter
add_action( 'the_content', 'wpb_author_info_box' );
// Allow HTML in author bio section
remove_filter('pre_user_description', 'wp_filter_kses');

步骤二、增加 CSS 样式到当前主题样式表文件。

.author_bio_section{
background: none repeat scroll 0 0 #F5F5F5;
padding: 15px;
border: 1px solid #ccc;
}

.author_name{
font-size:16px;
font-weight: bold;
}

.author_details img {
border: 1px solid #D8D8D8;
border-radius: 50%;
float: left;
margin: 0 10px 10px 0;
}

增加完成后就可以在文章页面查看效果,不同主题需要微调才可以达到最佳效果。下图底部效果就是老俍调整完的样式。

20190418_02.png

此方法唯一缺点就是不能增加“本文链接”输出,因为个人说明里面只支持 html 但不能调用函数。如果一定想要“本文链接”的请往下看。

方法二:直接把转载和版权信息写到当前主题的 functions.php 当中,就可以在每篇文章的最后显示了。缺点不好看,我也不会改成好看的样式,但是有“本文链接”输出。

function feed_copyright($content) {
    if(is_single() or is_feed()) {
        $content.= '<div>转载请注明来源:<a rel="bookmark" title="'.get_the_title().'" href="'.get_permalink().'">'.get_the_title().'</a></div>';
        $content.= '<div>本文链接地址:<a rel="bookmark" title="'.get_the_title().'" href="'.get_permalink().'">'.get_permalink().'</a></div>';
        $content.= '<div>订阅本站:<a title="俍注" href="http://www.oneinf.com/rss">http://www.oneinf.com/rss</a></div>';
        $content.= "</blockquote>";
    }
    return $content;
}
add_filter ('the_content', 'feed_copyright');

方法三:在当前模板的 single.php 文件中增加转载或版权信息。

找到外观 >> 编辑 single.php 文件,找到类似 div class=”content” 的源码,在下面合适的位置(多试两次)增加下面的代码。

本文由<?php the_author_posts_link(); ?>,转载请注明转自:<?php bloginfo('name'); ?><a href="<?php echo get_settings('home'); ?>"><?php echo get_settings('home'); ?></a>;
如果你觉得本博内容不错,欢迎 <a href="http://www.oneinf.com" target="_blank">订阅我的博客</a>,以便第一时间了解本博更新内容;
本文链接:<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_permalink(); ?></a>

当然,为了省事也可以通过插件直接实现。《WordPress文章版权插件:Add Post URL

本站 [ 俍注 ] 内除注明转载文章,其他均为老俍独立创作,采用「CC BY-NC-ND 4.0」创作共享协议。
原创不易,希望保留原文链接转载,原文链接:https://oneinf.com/tech/3362.html
(0)
打赏 微信打赏 微信打赏 支付宝打赏 支付宝打赏
上一篇 2019-04-18
下一篇 2019-06-26

相关阅读

回复 老俍

您的电子邮箱地址不会被公开。 必填项已用*标注

评论列表(12条)

  • Gabe
    Gabe 2023-03-01

    https://docs.fuukei.org/

    你可以了解一下这个主题功能够强大, 很多都不用插件.

  • Rares
    Rares 2019-04-25

    技术派啊

  • 老派
    老派 2019-04-22

    总结的很好啊,插入函数的方法是很灵活的,基本可以实现任何东西。

  • 知识共享网
    知识共享网 2019-04-21

    学习了,嘿嘿

  • 大致
    大致 2019-04-19

    此方法唯一缺点就是不能增加“本文链接”输出,因为个人说明里面只支持 html 但不能调用函数。
    ===============
    这有什么难的,方法2的第4行加到方法1的23行,大概这个意思吧。

    方法1的代码跟你的图片完全不是一回事。
    方法3的第2行错了。

    我觉得我做代码review都做出职业病了。

    • 老俍
      老俍 2019-04-19

      @大致你真是犹如“沃兹”般存在,兄弟!!!

      我的样式是按照方法1删减的,为了方便大家修改,我就没放删减后的代码。方法3的第2行错在哪里了?

    • 大致
      大致 2019-04-20

      @老俍href=后面的内容哪去了?

    • 老俍
      老俍 2019-04-24

      @大致对了,这个其实是留给用的人自己填网址的地方,应该标注一下。

    • 大致
      大致 2019-04-28

      @老俍订阅的话,可能用 bloginfo(‘rss2_url’)更合适一些。