WordPress在每篇文章内容上方和下方添加指定内容的方法
有时需要在WordPress每篇文章的开头部分或者结束部分插入固定的内容,比如广告代码、版权声明、打赏二维码之类的,一种方法是直接打开single.php文章模板添加,另一种方法是在WordPress函数模板functions.php里添加更方便。
function zm_content_insert( $return = 0 ) {// 插入的内容 $str.= "<div class='same'>"; $str.= "<h4>标题</h4>"; $str.= "<p>关注:<a href='http://zhangwenbao.com/' rel='external nofollow' target='_blank'>保哥笔记</a></p>"; $str.= "</div>"; if ($return) { return $str; } else { echo $str; } } function zm_content_filter($content) { if(!is_feed() && !is_home() && is_singular() && is_main_query()) { $content .= zm_content_insert(0);// 0在正文上面 //$content .= zm_content_insert(1);//1在正文下面 } return $content; } add_filter('the_content','zm_content_filter');
将以上代码复制粘贴到当前主题模板的functions.php里即可,如果要在文章前显示,就注释掉$content .= zm_content_insert(1);即可,如果要在文章后显示就注释掉$content .= zm_content_insert(0);即可。
- WordPress自动重命名媒体库图片文件名
- WordPress禁止HTTP_USER_AGENT恶意采集与攻击
- WordPress压缩html代码提升网页加载速度
- WordPress删除文章时同步删除文章图片
- 利用WordPress内置函数调用网站各种统计数据
- 添加扩展代码到WordPress核心文件functions.php更好的技巧
- WordPress判断用户邮箱地址来实现Gravatar头像本地化
- Wordpress中http.php文件wp_http_validate_url函数对输入IP验证不当漏洞
- 使用WordPress条件判断函数在特定页面执行特定代码
- WordPress取消加载Google的dns-prefetch以及s.w.org
本文标题:《WordPress在每篇文章内容上方和下方添加指定内容的方法》
本文链接:https://zhangwenbao.com/wordpress-adds-the-method-of-specifying-content-at-the-top-and-bottom-of-each-article.html