如何在WordPress正文开头和末尾分别添加固定内容

在不修改主题正文模板文件的情况下,想在正文开头和末尾添加固定内容,或者在已发表的文章中添加固定内容又不想重新编辑文章,可以通过下面的代码实现。

会用到过滤器钩子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;

}

(0)

相关推荐

分享本页
返回顶部