加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

WordPress单日志添加AD及sidebar小工具的应用

发布时间:2020-12-14 14:18:48 所属栏目:wordpress 来源:网络整理
导读:1、单日志添加AD(如Google AD) WordPress中single.php是单篇日志模板,如果需要在单篇日志中加入GoogleAdSense或是其他广告,这样做。 找到你所在主题中single.php如下代码 ?php the_content(); ? 在其前面加入 ! AD START div style=float:right;margin-l
1、单日志添加AD(如Google AD)

WordPress中single.php是单篇日志模板,如果需要在单篇日志中加入GoogleAdSense或是其他广告,这样做。

找到你所在主题中single.php如下代码

<?php the_content(); ?>

在其前面加入

<!– AD START –>
<div style=”float:right;margin-left:5px;”>
此处放入广告代码
</div>
<!– AD END –>

这样修改后效果是广告在日志右上角显示,效果在我的日志中就可以看到。

如果要改为左上角显示可以在<?php the_content(); ?>前面加入如下代码(这些都是DIV+CSS的应用,有基础的朋友可以根据情况自己修改)

<!– AD START –>
<div style=”float:left;margin-right:5px;”>
此处放入广告代码
</div>
<!– AD END –>

2、WordPress中sidebar小工具的应用

现在大部分WordPress主题都包含了小工具(widget),本文主要介绍下WordPress中小工具的原理。

A sidebar中只有1个widget

(1) WordPress主题所在目录中functions.php中的如下代码是注册一个widget

if ( function_exists(‘register_sidebar’) )
register_sidebar(array(
‘before_widget’ => ‘<div id=”%1$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h3>’,
‘after_title’ => ‘</h3>’,
));

(2) sidebar中的如下代码是用来显示这些注册过的widget中的内容

<?php if ( !function_exists(‘dynamic_sidebar’)|| !dynamic_sidebar() ) : ?>
<?php endif; ?>

(3)有了这些代码后我们就可以在WordPress后台的小工具中随意添加代码了。

另外如果没有(1)中的代码,那么在后台就查看不到小工具了,此时就相当于主题不支持widget,如果没有(2)中的代码,在WordPress后台能添加widget,但是在WordPress中不能显示这些widget中的内容。

B sidebar中有多个widget

(1) 和只有1个widget情况类似,只是functions.php中相关代码改为如下(此例中有两个widget,名字分别为widget1 widget2)

if( function_exists(‘register_sidebar’) ) {
register_sidebar(array(
’name’ => ‘widget1′,
’before_widget’ => ‘<div id=”%1$s”>’,
’after_widget’ => ‘</div>’,
’before_title’ => ‘<h3>’,
’after_title’ => ‘</h3>’
));
register_sidebar(array(
’name’ => ‘widget1′,
’after_title’ => ‘</h3>’
));
}

(2) sidebar中的如下代码是显示注册过的widget1和widget2中的内容

<?php if ( !function_exists(‘dynamic_sidebar’)|| !dynamic_sidebar(widget1) ) : ?>
<?php endif; ?>

<?php if ( !function_exists(‘dynamic_sidebar’)|| !dynamic_sidebar(widget2) ) : ?>
<?php endif; ?>

注:在以上A和B中的步骤(1)中register_sidebar的参数要因主题的不同需要做修改,用法可以参考http://codex.wordpress.org/Function_Reference/register_sidebar

原文摘自 http://www.zenoven.com/useful/2010021238.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读