用户
ID: 剩余积分:
积分仅限于AI文章写作也可以用于WordPress下的SEO合集插件“智能改写”“词库挖掘”“关键词排名监控”“AI智能DK”功能使用;
充值仅用于消费,不可变更,退款,提现,请慎重选择!
用户邮箱
验证码
暂无数据
要实现添加代码首先要认识wordpress添加事件的钩子函数add_action($event,$func)。字面意思添加一个操作,一个行动。
$event参数是钩子事件,一般是执行的时机。例如:after_setup_theme主题启动执行,publish_post发布文章执行。$func自定义的函数或者类的方法。
今天我们讲解wp_head加载前端页面头部时执行(或者说往头部添加内容,或者去除内容),wp_footer底部添加内容。
add_action( 'wp_head', [$this,'baiduseo_mainpage'],1 );
public function baiduseo_mainpage(){
$baiduseo_zz = get_option('baiduseo_zz');
if(isset($baiduseo_zz['toutiao_key']) && $baiduseo_zz['toutiao_key'] ){
echo '<script>
/*seo合集头条推送*/
(function(){
var el = document.createElement("script");
el.src = "https://sf1-scmcdn-tos.pstatp.com/goofy/ttzz/push.js?'.esc_attr($baiduseo_zz['toutiao_key']).'";
el.id = "ttzz";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(el, s);
})(window)
</script>';
}
if(is_home() || is_front_page()){
$seo = get_option('seo_init');
if($seo){
if(isset($seo['keywords']) && $seo['keywords']){
echo '<meta name="keywords" content="'.esc_attr($seo['keywords']).'">'."\n\r";
}
if(isset($seo['description']) && $seo['description']){
echo '<meta name="description" content="'.esc_attr($seo['description']).'">'."\n\r";
}
}
}elseif(is_category()){
$cate = get_the_category();
if(isset($cate[0]->cat_ID)){
$seo = get_option('baiduseo_cate_'.$cate[0]->cat_ID);
if(!empty($seo)){
if(isset($seo['keywords']) && $seo['keywords']){
echo sprintf('<meta name="keywords" content="%s" />'."\n",esc_attr($seo['keywords']));
}
if(isset($seo['description']) && $seo['description']){
echo sprintf('<meta name="description" content="%s" />'."\n",esc_attr($seo['description']));
}
}
}
}elseif(is_single()){
add_action( 'the_content', [$this,'BaiduSEO_addlink']);
}
}上面的代码是在头部head标签中添加头条推送的js,设置页面的keywords、description,以及加载头部时,更改文章内页的内容。wp_footer钩子的用法与wp_head相似,不过是body标签的底部添加。