Suporte » Questões avançadas » Inserir div no meio do loop

  • Resolvido Gdiniz

    (@gdiniz)


    Olá a todos estou com um problema em um site que estou desenvolvendo, o que acontece é que preciso inserir uma div no meio do loop porem essa div n pode se repetir e não estou conseguindo fazer, veja como está o meu loop:

    <?php wp_reset_query();?>
    <?php if (have_posts()) : $postcount = 0;  while (have_posts()) : the_post(); $postcount++; ?>
    <?php if ($postcount <= 2) : ?>
    
    <div id="destaque2">
    <h2><a>" title="<?php printf(__('Permalink to %s', 'criarnet'), esc_html(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h2>
    <div class="thumb">
        <a rel="nofollow">"><?php
    //Pega as imagens anexadas desse post
    $args = array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => -1,
            'order' => 'ASC',
    	'post_status' => null,
    	'post_parent' => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		$img = wp_get_attachment_thumb_url( $attachment->ID );
                    break;
            }
    }
    echo "<img src='$img' alt='Jornal Alecrim Lagoa Santa MG' width='300' height='150' />";
    ?></a></div>
    <p><?php ob_start(); the_excerpt(); echo dp_clean(ob_get_clean(), 100); ?> [...]</p>
    </div>
    
    <?php else: ?>
    
    <h3><a>" title="<?php printf(__('Permalink to %s', 'criarnet'), esc_html(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h3>
    <div id="margem"></div>
    
    <?php endif; //terminal a condicional de posts/excerpt ?>
    <?php endwhile;?>
    
    <?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php else : ?>
    
    <?php endif; ?>

    Preciso inserir uma div que irá cercar os posts que aparecerem entre as tags h3 porem esse div não pode se repetir… tentei assim:

    <?php wp_reset_query();?>
    <?php if (have_posts()) : $postcount = 0;  while (have_posts()) : the_post(); $postcount++; ?>
    <?php if ($postcount <= 2) : ?>
    
    <div id="destaque2">
    <h2><a>" title="<?php printf(__('Permalink to %s', 'criarnet'), esc_html(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h2>
    <div class="thumb">
        <a rel="nofollow">"><?php
    //Pega as imagens anexadas desse post
    $args = array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => -1,
            'order' => 'ASC',
    	'post_status' => null,
    	'post_parent' => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		$img = wp_get_attachment_thumb_url( $attachment->ID );
                    break;
            }
    }
    echo "<img src='$img' alt='Jornal Alecrim Lagoa Santa MG' width='300' height='150' />";
    ?></a></div>
    <p><?php ob_start(); the_excerpt(); echo dp_clean(ob_get_clean(), 100); ?> [...]</p>
    </div>
    <strong><div id="destaque3"></strong>
    <?php else: ?>
    
    <h3><a>" title="<?php printf(__('Permalink to %s', 'criarnet'), esc_html(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h3>
    <div id="margem"></div>
    
    <?php endif; //terminal a condicional de posts/excerpt ?>
    <?php endwhile;?>
    <strong></div></strong>
    
    <?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php else : ?>
    
    <?php endif; ?>

    Porem a div acaba se repetindo em volta de todo h3 e bagunça todo o layout além de não resolver meu problema…

    Alguém me da uma luz?

A visualizar 2 respostas - de 1 a 2 (de um total de 2)
  • Olá,

    Aqui vai a minha sugestão (não testei):

    <?php wp_reset_query();?>
    <?php if (have_posts()) : $postcount = 0;  while (have_posts()) : the_post(); $postcount++; ?>
    <?php if ($postcount <= 2) : ?>
    
    <div id="destaque2">
    <h2><a>" title="<?php printf(__('Permalink to %s', 'criarnet'), esc_html(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h2>
    <div class="thumb">
        <a rel="nofollow">"><?php
    //Pega as imagens anexadas desse post
    $args = array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => -1,
            'order' => 'ASC',
    	'post_status' => null,
    	'post_parent' => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		$img = wp_get_attachment_thumb_url( $attachment->ID );
                    break;
            }
    }
    echo "<img src='$img' alt='Jornal Alecrim Lagoa Santa MG' width='300' height='150' />";
    ?></a></div>
    <p><?php ob_start(); the_excerpt(); echo dp_clean(ob_get_clean(), 100); ?> [...]</p>
    </div>
    
    <?php else: ?>
    
    <?php if($postcount == 3): ?>
    	<!-- Inicio dos h3 -->
    	<div id="destaque3">
    <?php endif; ?>
    
    <h3><a>" title="<?php printf(__('Permalink to %s', 'criarnet'), esc_html(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h3>
    <div id="margem"></div>
    
    <?php endif; //terminal a condicional de posts/excerpt ?>
    <?php endwhile;?>
    
    <?php if($postcount > 2): ?>
    	<!-- Fim dos h3 -->
    	</div>
    <?php endif; ?>
    
    <?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    <?php else : ?>
    
    <?php endif; ?>

    Na tua solução tens 2 problemas:
    – Onde inicias o teu div#destaque3 é uma zona do ciclo que é lida 3 vezes, pois a condição do if é quando o postcount for menor ou igual 2
    – Estás a colocar um strong à volta de uma div aberta. Não podes fechar uma tag com outra lá dentro por fechar

    Thread Starter Gdiniz

    (@gdiniz)

    Perfeito dbeja muito obrigado!

A visualizar 2 respostas - de 1 a 2 (de um total de 2)
  • O tópico ‘Inserir div no meio do loop’ está fechado a novas respostas.