【发布时间】:2015-09-09 20:17:19
【问题描述】:
我的 wordpress 主题中有以下循环,它显示帖子中的所有附加图像。我想要做的是在帖子中的 3 个附件之后插入一个谷歌广告代码。
<?php function show_attachments(){
global $post;
while( have_posts () ){
the_post();
$post_id = $post -> ID
?>
<div class="featimg" >
<div class="img">
<?php
$img_src = wp_get_attachment_image_src( $post_id , 'full' );
echo '<img src="'.$img_src[0].'" alt="" />';
?>
</div>
</div>
<?php
}
}
$layout = new LBSidebarResizer( 'attachment' );
$layout -> render_frontend( 'show_attachments' );
?>
我想我必须做一些与此类似的事情:
<?php
$i = 0;
function show_attachments(){
global $post;
while( have_posts () ){
$i++;
the_post();
if ($i == 3){
echo 'google ads code here';
};
$post_id = $post -> ID
?>
<div class="featimg" >
<div class="img">
<?php
$img_src = wp_get_attachment_image_src( $post_id , 'full' );
echo '<img src="'.$img_src[0].'" alt="" />';
?>
</div>
</div>
<?php
}
}
$layout = new LBSidebarResizer( 'attachment' );
$layout -> render_frontend( 'show_attachments' );
?>
你们能帮我进一步吗?
【问题讨论】:
-
那么你的代码有什么问题?
-
它不起作用,看起来这不是我正在寻找的正确循环。我找不到带有正确循环的正确文件,用于在单个 wordpress 帖子中显示附加图像。任何想法如何找到它?
-
这怎么行不通?这是否会显示帖子,但不会与您的谷歌广告相呼应?还是循环中断了,没有帖子显示?
-
看来这不是在我的帖子中显示图像的循环。即使我在没有任何条件的情况下在函数中放置了一个 ech“测试”,它也不会出现在我的页面上。
标签: php wordpress loops insert ads