Z-Blog PHP在文章列表任意位置插入广告的方法

时间:2022-05-06 浏览:622 分类:免费建站教程

1、新建广告文件


在模板文件夹内建一个文件作为放广告代码的文件(这里暂且命名为:post-ad.php)


2、修改模板文件


找到 \zb_users\theme\default\template\index.php (default根据你自己的模板为准)这个文件找到下面这段代码:{foreach $articles as $article}


{if $article.IsTop}


{template:post-istop}


{else}


{template:post-multi}


{/if}


{/foreach}


如果你只想在置顶列表里加入广告那么将上面的代码替换为:{php}$j=1;{/php}


{foreach $articles as $article}


{if $article.IsTop}


{template:post-istop}


{if $j==3}


{template:post-ad}


{/if}


{php}$j++;{/php}


{else}


{template:post-multi}


{/if}


{/foreach}


备注:数字3为广告出现的位置,post-ad 为第一步建的文件名,下同。


如果你想在除置顶列表外加入广告那么将上面的代码替换为:{php}$i=1;{/php}


{foreach $articles as $article}


{if $article.IsTop}


{template:post-istop}


{else}


{template:post-multi}


{if $i==3}


{template:post-ad}


{/if}


{php}$i++;{/php}


{/if}


{/foreach}


上面两个位置同时出现的代码为:{php}$i=1;$j=1;{/php}


{foreach $articles as $article}


{if $article.IsTop}


{template:post-istop}


{if $j==3}


{template:post-ad}


{/if}


{php}$j++;{/php}


{else}


{template:post-multi}


{if $i==3}


{template:post-ad}


{/if}


{php}$i++;{/php}


{/if}


{/foreach}