Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将DIV插入WordPress相关帖子的中间部分_Php_Wordpress - Fatal编程技术网

Php 将DIV插入WordPress相关帖子的中间部分

Php 将DIV插入WordPress相关帖子的中间部分,php,wordpress,Php,Wordpress,如何在wordpress循环的第三篇文章(下面的代码)后插入div?? 我搜索了这个网站,但找不到如何将其插入像我这样的代码的解释 <?php $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' =&g

如何在wordpress循环的第三篇文章(下面的代码)后插入div?? 我搜索了这个网站,但找不到如何将其插入像我这样的代码的解释

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'orderby'   => 'rand',
'showposts'=>6, //  
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div  id="loopindex">';
while ($my_query->have_posts()) {
$my_query->the_post();

?>
<div class="post_mobile" id="post-<?php the_ID(); ?>">
 CONTENT
</div>



<?php
}
echo '</div>';
}
}
?>

您只需要一个
计数器
!在
while循环开始之前,初始化一个计数器变量

然后在
while循环开始时
检查计数器值,并在
while循环结束时
向其添加
1
。像这样:

$your\u query=新的wp\u查询($args);
$counter=1;
while($your\u query->have\u posts()){
//首先检查计数器变量的值
如果(3==$计数器){
//在此处输出您的div/html
}
//如果你需要的话,在循环中做你的事情!
//在while循环的末尾,向计数器添加1
$counter++;
}

使用WordPress
current\u post
检查while循环中的post索引

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
    'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
    'orderby'   => 'rand',
    'showposts'=>6, //
    'caller_get_posts'=>1
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
        echo '<div  id="loopindex">';
        while ($my_query->have_posts()) {
            $my_query->the_post();
            // Get the current post index
            $current_post = $my_query->current_post;

            //Check if post is 4th post
            if($current_post == 3){?>
               <div>This content is displayed only after third post.</div>
            <?php
            }
            ?>
            <div class="post_mobile" id="post-<?php the_ID(); ?>">
             CONTENT
            </div>
        <?php
        }
    echo '</div>';
    }
}
?>

此内容仅在第三次发布后显示。