Home> CMS Tutorial> WordPress> body text

How to automatically add an author information box at the end of a WordPress post

藏色散人
Release: 2019-12-20 13:31:35
forward
2978 people have browsed it

The following columnWordPress Tutorialwill introduce to you how to automatically add an author information box at the end of a WordPress article. I hope it will be helpful to friends in need!

How to automatically add an author information box at the end of a WordPress post

If you want to add relevant information about the author of the article at the end of theWordPressarticle, the following code can be conveniently included in the article Add an author info box.

Add the code to the current theme functions.php:

function wp_author_info_box( $content ) { global $post; // 检测文章与文章作者 if ( is_single() && isset( $post->post_author ) ) { // 获取作者名称 $display_name = get_the_author_meta( 'display_name', $post->post_author ); // 如果没有名称,使用昵称 if ( empty( $display_name ) ) $display_name = get_the_author_meta( 'nickname', $post->post_author ); // 作者的个人信息 $user_description = get_the_author_meta( 'user_description', $post->post_author ); // 获取作者的网站 $user_website = get_the_author_meta('url', $post->post_author); // 作者存档页面链接 $user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author)); if ( ! empty( $display_name ) ) $author_details = '
关于 ' . $display_name . '
'; if ( ! empty( $user_description ) ) // 作者头像 $author_details .= '
' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '
'; $author_details .= ''; } else { // 如果作者没有填写网站则不显示网站链接 $author_details .= '
'; } // 在文章后面添加作者信息 $content = $content . '
' . $author_details . '
'; } return $content; } // 添加过滤器 add_action( 'the_content', 'wp_author_info_box' ); // 允许HTML remove_filter('pre_user_description', 'wp_filter_kses'); 再将配套的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; }
Copy after login

The above is the detailed content of How to automatically add an author information box at the end of a WordPress post. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!