如何自动添加内容到 WordPress 文章尾部

如何自动添加内容到 WordPress 文章尾部,第1张

在loop内或者loop外都可以。这个是比较自由一点的方法。

loop内主要是可以自定义内容,可以通过字段来实现,放在循环结束前的最后

loop外也可以通过函数或者固定的html添加到尾部

通过函数可参考

function insertFootNote($content) { 

if(!is_feed() && !is_home()) { 

$content.= "<div class='subscribe'>" 

$content.= "<h4>Enjoyed this article?</h4>" 

$content.= "<p>Subscribe to our <a href='http://feed.imbolo.com/'>RSS feed</a> and never miss a recipe!</p>" 

$content.= "</div>" 

return $content 

add_filter ('the_content', 'insertFootNote')

函数可通过option控制变量输出一样达到自定义输出内容的效果。条件判断加上is_single页面判断替换上边的排除判断。

可以将下面的代码添加到主题的 functions.php 文件中:

//在所有文章底部添加自定义内容

function add_after_post_content($content) {

if(!is_feed() &&!is_home() &&is_singular() &&is_main_query()) {

$content .= '你需要添加的自定义内容'

}

return $content

}

add_filter('the_content', 'add_after_post_content')

第 3 行代码使用了条件标签,禁止Feed和首页输出自定义内容。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11719525.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存