欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress 文章页面开头或末尾如何添加固定内容?

作者 : 自学哈 本文共1377个字,预计阅读时间需要4分钟 2022-11-6 共134人阅读
也想出现在这里? 联系我们

很多站长朋友WordPress写文章的时候,想在文章底部留下自己的版权信息,如:“XXX授权,转载请注明”。

场景二,已经发布的文章,不想每篇都打开去更改。

自己手动添加太过麻烦,有没有可以通过修改使得他固定添加内容呢?其实很多网上的模板是支持这个功能的,但是大部分是收费模板。

这里先感谢此方法的分享者。

方法

这里我们会用到过滤器钩子add_filter。

add_filter( 'the_content', '' )

然后将以下代码复制到当前使用模板的functions.php中

在文章内容开头添加固定内容

add_filter('the_content', 'add_zm_content_beforde');function add_zm_content_beforde( $content ) { if( !is_feed() && !is_home() && is_singular() && is_main_query() ) { $before_content = '在文章内容开头添加固定内容'; $zm = $before_content . $content; } return $zm;}

在文章内容末尾添加固定内容

add_filter('the_content', 'add_zm_content_after');function add_zm_content_after( $content ) { if( !is_feed() && !is_home() && is_singular() && is_main_query() ) { $after_content = '在文章内容末尾添加固定内容';  $zm = $content . $after_content; } return $zm;}

在开头和末尾同时添加固定内容

add_filter('the_content', 'add_zm_content_before_and_after');function add_zm_content_before_and_after( $content ) { if( !is_feed() && !is_home() && is_singular() && is_main_query() ) { $after_content = '在文章内容末尾添加固定内容';  $before_content = '在文章内容开头添加固定内容'; $zm = $before_content . $content . $after_content; } return $zm;}

在自定义文章类型“books”文章末尾添加固定内容(或者其他类型)

add_filter('the_content', 'add_zm_content_after_books_custom_post_type');function add_zm_content_after_books_custom_post_type( $content ) { if (is_singular( "books" )){ $new_books_content = '只在自定义帖子类型“books”文章末尾添加固定内容'; $aftercontent = $new_books_content; $zm = $content . $aftercontent; return $zm; } else { return $content; }}

这个方法是非常实用的功能,你可以为自己的网站文章添加引导文,或者其他的广告信息。

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress 文章页面开头或末尾如何添加固定内容?
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号