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的應(yīng)用,有基礎(chǔ)的朋友可以根據(jù)情況自己修改)
<!– AD START –>
<div style=”float:left;margin-right:5px;”>
此處放入廣告代碼
</div>
<!– AD END –>
2、WordPress中sidebar小工具的應(yīng)用
現(xiàn)在大部分WordPress主題都包含了小工具(widget),本文主要介紹下WordPress中小工具的原理。
A sidebar中只有1個(gè)widget
(1) WordPress主題所在目錄中functions.php中的如下代碼是注冊(cè)一個(gè)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中的如下代碼是用來(lái)顯示這些注冊(cè)過的widget中的內(nèi)容
<?php if ( !function_exists(‘dynamic_sidebar’)|| !dynamic_sidebar() ) : ?>
<?php endif; ?>
(3)有了這些代碼后我們就可以在WordPress后臺(tái)的小工具中隨意添加代碼了。
另外如果沒有(1)中的代碼,那么在后臺(tái)就查看不到小工具了,此時(shí)就相當(dāng)于主題不支持widget,如果沒有(2)中的代碼,在WordPress后臺(tái)能添加widget,但是在WordPress中不能顯示這些widget中的內(nèi)容。
B sidebar中有多個(gè)widget
(1) 和只有1個(gè)widget情況類似,只是functions.php中相關(guān)代碼改為如下(此例中有兩個(gè)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′,
’before_widget’ => ‘<div id=”%1$s”>’,
’after_widget’ => ‘</div>’,
’before_title’ => ‘<h3>’,
’after_title’ => ‘</h3>’
));
}
(2) sidebar中的如下代碼是顯示注冊(cè)過的widget1和widget2中的內(nèi)容
<?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的參數(shù)要因主題的不同需要做修改,用法可以參考http://codex.wordpress.org/Function_Reference/register_sidebar
原文摘自 http://www.zenoven.com/useful/2010021238.html