Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

pedrohf

Loops com WP_QUERY() / paginação repetida

Recommended Posts

Olá, pessoal!
Estou fazendo um blog e tive que usar 6 loops na página inicial.
Um para o slideshow, um para os posts num box com apenas um categoria exibida, um nos Top 5 com os posts mais acessados. Nesses três eu usei query_posts e tá tudo certo, não obtive problemas.
Porém na parte principal, com todos os posts de todas as categorias, eu tive que usar mais três loops, mas dessa vez com WP_query.
1. primeiro post, com uma formatação e estilo, com um banner embaixo.
2. segundo e terceiro post, com a mesma formatação e estilo do primeiro, com outro banner embaixo.
3. demais posts, com formatação e estilo diferente dos 3 primeiros.
O problema disso tudo é a paginação! Na página 2 tá ficando tudo repetido, talvez porque eu tive que usar esses três loops na parte principal e, além disso, usando o parâmetro offset.
Segue abaixo o conteúdo da minha index.php:
<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme and one of the
 * two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * For example, it puts together the home page when no home.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

get_header(); ?>

<!-- CONTEUDO GERAL / DIV QUE ENGLOBA TODO O CONTEUDO DE POSTS E SIDEBAR -->

		<div id="container">

        <!-- CONTEUDO CONTEUDO DE POSTS -->
        <div id="container-posts">

            <!-- CONTEUDO POSTS GRANDES -->
            <?php
			$args = array(
		    'posts_per_page' => '1',
			'offset' => 0,
			);

			$grandes1 = new WP_Query( $args);
			if (have_posts()) : while($grandes1->have_posts()) : $grandes1->the_post(); ?>

            <div id="container-posts-grande">
            	<div id="imagem-post-grande"><a href="<?php the_Permalink(); ?>#post" title="<?php the_title(); ?>"><?php the_post_thumbnail('post-grande'); ?></a></div>
                <div id="textos-post-grande">
                		<div id="info-post-grande"><span class="data-post-grande"><?php echo get_the_time('M d, Y'); ?></span> | <span class="comentarios-post-grande"><?php comments_popup_link('Comentários', '1 Comentário', '% Comentários'); ?></span></a></div>
                        <div id="titulo-post-grande"><a href="<?php the_Permalink(); ?>#post" title="<?php the_title(); ?>"><?php title_limite(63); ?></a></div>
                        <div id="categoria-post-grande"><?php exclude_post_categories("1, 11, 12"); ?></div>
                        <div id="conteudo-post-grande"><?php echo excerpt(50); ?></div>
                  <div id="continue-lendo-post-grande"><a href="<?php the_Permalink(); ?>#post">continue lendo esse post ></a></div>
              </div>
        	</div>
            <?php endwhile; else: ?>
   			<?php endif; ?>
        	<!-- FIM CONTEUDO POSTS GRANDES -->

            <!-- BANNER -->
            <div id="container-banner">
            	<div id="aviso-publicidade">PUBLICIDADE</div>
            	<div id="imagem-banner"><?php if ( function_exists( 'useful_banner_manager_banners' ) ) { useful_banner_manager_banners( '1', 1 ); } ?></div>
            </div>
        	<!-- FIM BANNER -->

            <!-- CONTEUDO POSTS GRANDES -->
            <?php
			$args = array(
		    'posts_per_page' => '2',
			'offset' => 1,
			);

			$grandes2 = new WP_Query( $args);
			if (have_posts()) : while($grandes2->have_posts()) : $grandes2->the_post(); ?>

            <div id="container-posts-grande">
            	<div id="imagem-post-grande"><a href="<?php the_Permalink(); ?>#post" title="<?php the_title(); ?>"><?php the_post_thumbnail('post-grande'); ?></a></div>
                <div id="textos-post-grande">
                		<div id="info-post-grande"><span class="data-post-grande"><?php echo get_the_time('M d, Y'); ?></span> | <span class="comentarios-post-grande"><?php comments_popup_link('Comentários', '1 Comentário', '% Comentários'); ?></span></a></div>
                        <div id="titulo-post-grande"><a href="<?php the_Permalink(); ?>#post" title="<?php the_title(); ?>"><?php title_limite(63); ?></a></div>
                        <div id="categoria-post-grande"><?php exclude_post_categories("1, 11, 12"); ?></div>
                        <div id="conteudo-post-grande"><?php echo excerpt(55); ?></div>
                  <div id="continue-lendo-post-grande"><a href="<?php the_Permalink(); ?>#post">continue lendo esse post ></a></div>
              </div>
        	</div>
            <?php endwhile; else: ?>
   			<?php endif; ?>
        	<!-- FIM CONTEUDO POSTS GRANDES -->

             <!-- BANNER -->
            <div id="container-banner">
            	<div id="aviso-publicidade">PUBLICIDADE</div>
            	<div id="imagem-banner"><?php if ( function_exists( 'useful_banner_manager_banners' ) ) { useful_banner_manager_banners( '2', 1 ); } ?></div>
            </div>
        	<!-- FIM BANNER -->

            <!-- CONTEUDO POSTS MEDIO -->
            <?php
			$args = array(
		    'posts_per_page' => '3',
			'offset' => 3,
			);

			$medios = new WP_Query( $args);
			if (have_posts()) : while($medios->have_posts()) : $medios->the_post(); ?>

            <div id="container-posts-medio">
            	<div id="imagem-post-medio"><a href="<?php the_Permalink(); ?>#post" title="<?php the_title(); ?>"><?php the_post_thumbnail('post-medio'); ?></a></div>
                <div id="textos-post-medio">
                		<div id="info-post-medio"><span class="data-post-medio"><?php echo get_the_time('M d, Y'); ?></span> | <span id="categoria-post-medio"><?php exclude_post_categories("1, 11, 12"); ?></span></div>
                        <div id="titulo-post-medio"><a href="<?php the_Permalink(); ?>#post" title="<?php the_title(); ?>"><?php title_limite(45); ?></a></div>
                  <div id="conteudo-post-medio"><?php echo excerpt(25); ?> </div>
              </div>
        	</div>
            <?php endwhile; else: ?>
   			<?php endif; ?>
        	<!-- FIM CONTEUDO POSTS MEDIO -->

            <div id="navegacao">
            <div class="nav-previous-alignleft"><?php next_posts_link( '< ANTIGOS' ); ?></div>
			<div class="nav-next-alignright"><?php previous_posts_link( 'RECENTES >' ); ?></div>
            </div>

       </div>
        <!-- FIM CONTEUDO DE POSTS -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
Usei três loops nessa parte principal por precisar inserir esses dois banners no meio dos três primeiros posts e depois por precisar alterar o estilo dos boxes de posts.
Alguém pode me ajudar, por favor?

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Não entendi o problema exatamente, mas a paginação embutida do WP só pode funcionar com um loop, você pode ter outros, mas só um deles deverá paginar.

 

Além disso, como você está criando o WP_Query na mão, você também tem que especificar a página da query. Você pega a página com get_page_var, exemplo:

 

 

<?php
$paged = get_query_var( 'paged', 1);
$args = array(
         'posts_per_page' => '2',
         'page' => $paged
);

$postsPaginado = new WP_Query( $args);

 

Eu removi a opção offset porque não era necessário.

Compartilhar este post


Link para o post
Compartilhar em outros sites

lucaswxp, perdão, esqueci de marcar aqui como resolvido.

 

usei um contador no loop.

o problema era que estava usando query posts com offset.

 

Obrigado mesmo assim.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.