如何在WordPress 文章未尾自动添加一个作者信息框 - 广告联盟评测网
  1. 广告联盟评测网 > 流量运营 >

如何在WordPress 文章未尾自动添加一个作者信息框

下面由wordpress/" target="_blank">wordpress教程栏目给大家介绍在wordpress 文章未尾自动添加一个作者信息框的方法,希望对需要的朋友有所帮助!

如果想在WordPress文章的末尾,添加文章作者的相关信息,下面一段代码可以方便在文章中添加一个作者的信息框。

将代码添加到当前主题functions.php中:

function wp_author_info_box( $content ) { global $post; // 检测文章与文章作者 if ( is_single() && isset( $post->post_author ) ) { // 获取作者名称 $display_name = get_the_author_meta( &#39;display_name&#39;, $post->post_author ); // 如果没有名称,使用昵称 if ( empty( $display_name ) ) $display_name = get_the_author_meta( &#39;nickname&#39;, $post->post_author ); // 作者的个人信息 $user_description = get_the_author_meta( &#39;user_description&#39;, $post->post_author ); // 获取作者的网站 $user_website = get_the_author_meta(&#39;url&#39;, $post->post_author); // 作者存档页面链接 $user_posts = get_author_posts_url( get_the_author_meta( &#39;ID&#39; , $post->post_author)); if ( ! empty( $display_name ) ) $author_details = &#39;<div class="author-name">关于 &#39; . $display_name . &#39;</div>&#39;; if ( ! empty( $user_description ) ) // 作者头像 $author_details .= &#39;<div class="author-details">&#39; . get_avatar( get_the_author_meta(&#39;user_email&#39;) , 90 ) . nl2br( $user_description ). &#39;</div>&#39;; $author_details .= &#39;<div class="author-links"><a href="&#39;. $user_posts .&#39;">查看 &#39; . $display_name . &#39; 所有文章</a>&#39;; // 检查作者在个人资料中是否填写了网站 if ( ! empty( $user_website ) ) { // 显示作者的网站链接 $author_details .= &#39; | <a href="&#39; . $user_website .&#39;" target="_blank" rel="nofollow">网站</a></div>&#39;; } else { // 如果作者没有填写网站则不显示网站链接 $author_details .= &#39;</div>&#39;; } // 在文章后面添加作者信息 $content = $content . &#39;<footer class="author-bio-section" >&#39; . $author_details . &#39;</footer>&#39;; } return $content; } // 添加过滤器 add_action( &#39;the_content&#39;, &#39;wp_author_info_box&#39; ); // 允许HTML remove_filter(&#39;pre_user_description&#39;, &#39;wp_filter_kses&#39;); 再将配套的CSS添加到主题样式文件style.css中: .author-bio-section { background: #fff; float: left; width: 100%; margin: 10px 0; padding: 15px; border: 1px dashed #ccc; } .author-name { font-size: 15px; font-weight: bold; margin: 0 0 5px 0; } .author-details img { float: left; width: 48px; height: auto; margin: 5px 15px 0 0; }登录后复制

这篇流量运营《如何在WordPress 文章未尾自动添加一个作者信息框》,目前已阅读次,本文来源于php中文,在2024-09-26发布,该文旨在普及流量运营知识,如果你有任何疑问,请通过网站底部联系方式与我们取得联系

  • 上一篇:dede怎么调取三级栏目名及栏目下的内容列表
  • 下一篇:织梦DEDECMS怎么用JS方式调用评论总数