Suporte » Plugins » Como usar dois contadores no loop?

  • Resolvido Marcusvot

    (@marcusvot)


    Olá estou tentando usar 2 contadores no loop, mas o máximo que consigo é que o tudo apareça no mesmo lugar:

    <!-- O loop -->
    <div class="row listing">
    <div class="row-listing-separation"></div>
    <?php $counter=0; ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php
    $counter++;
    if ($counter == 2) : ?>
    Exemplo ads1
    <?php endif; ?>
    <?php
    $counter++;
    if ($counter == 4) : ?>
    Exemplo ads2
    <?php endif; ?>
    <article class="fronta-article" itemscope itemtype="http://schema.org/Article">
    <a href="<?php the_permalink(); ?>" class="fronta-link-img" title="<?php the_title_attribute(); ?>">
    <div class="fronta-img"><?php the_post_thumbnail('gallery-block', array('class' => 'image', 'title' => strip_tags(get_the_title()), 'itemprop' => 'image')); ?> </div>
    </a>
    <div class="fronta-content">
    <h3 class="fronta-title" itemprop="name">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" itemprop="url"><?php the_title(); ?></a>
    </h3>
    <div class="fronta-info"><time datetime="<?php echo get_the_date(__('Y-m-d\TH:i:sP')); ?>" itemprop="datePublished"><?php echo get_the_date(); ?> </time></div>
    </header>
    </div>
    <div class="fronta-text"><?php echo the_excerpt(); ?></div>
    </article>
    <?php endwhile;  ?>

Visualizando 3 respostas - 1 até 3 (de um total de 3)
  • Não sei se entendi muito bem o que precisa, mas pelo código me parece que o segundo $counter++ está atrapalhando a contagem, pois ele passa no primeiro if, recebe + 1 e no segundo if recebe + 1.

    Se não for isso, diga qual a lógica do que precisa.

    Criador do tópico Marcusvot

    (@marcusvot)

    Olá Everaldo, o que quero e inserir dois anúncios, um depois do primeiro item do Loop, e outro depois do segundo item do Loop. Tentei de várias formas, mas não consegui.

    Criador do tópico Marcusvot

    (@marcusvot)

    Consegui achar o que eu queria no Fórum em inglês.
    Deixo a baixo o código para alguém que precisar no futuro:

    <!-- O loop -->
    <div class="row listing">
    <?php if (have_posts()) : $count = 0; while (have_posts()) : the_post(); $count++; ?>
        <?php if($count % 11 == 1) : ?>
        <div class="block bn1">< ..(ADS 1).. ></div>
        <?php endif; ?>
        <?php if($count % 11 == 2) : ?>
        <div class="block bn2">< ..(ADS 2).. ></div>
        <?php endif; ?>
        <?php if($count % 11 == 3) : ?>
        <div class="block bn3">< ..(ADS 3).. ></div>
        <?php endif; ?>
    <article class="fronta-article" itemscope itemtype="http://schema.org/Article">
    <a href="<?php the_permalink(); ?>" class="fronta-link-img" title="<?php the_title_attribute(); ?>">
    <div class="fronta-img"><?php the_post_thumbnail('gallery-block', array('class' => 'image', 'title' => strip_tags(get_the_title()), 'itemprop' => 'image')); ?> </div>
    </a>
    <div class="fronta-content">
    <h3 class="fronta-title" itemprop="name">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" itemprop="url"><?php the_title(); ?></a>
    </h3>
    <div class="fronta-info"><time datetime="<?php echo get_the_date(__('Y-m-d\TH:i:sP')); ?>" itemprop="datePublished"><?php echo get_the_date(); ?> </time></div>
    </header>
    </div>
    <div class="fronta-text"><?php echo the_excerpt(); ?></div>
    </article>
    <?php endwhile;  ?>

Visualizando 3 respostas - 1 até 3 (de um total de 3)
  • O tópico ‘Como usar dois contadores no loop?’ está fechado para novas respostas.