WordPress给每个文章开头或者末尾添加内容

有需求想要给每个文章开头或者末尾添加内容,百度搜了些代码,感觉好用,贴上来备忘。

使用方法:在主题文件的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 = '[c-alert type="info"]安装本站所有软件前请先关闭杀毒软件,本站所有软件绿色无毒,否则可能出现安装失败[/c-alert]'; $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; }}

添加代码后,如发现首页显示文章的描述为空,

在文章内容开头添加固定内容的代码可修改为下列代码,亲测有效

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;
    }
    
    return $content; // 返回原始的 $content
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值