Diversify posts in WordPress by categories

2

I'm using this code to display the news on the home page of a site, but I wanted to display:

4 news from Cat1 + 3 from Cat2 + 3 from Cat3

<!-- NOTÍCIAS AMB -->
          <?php 
          if (have_posts('order=DESC')) : while (have_posts('numberposts=10')) : the_post('category_name=cat1'); 
          ?>
          <div class="nto-destaq row columns">
            <div class="imgmin-noticias-box large-3">
              <div class="imgmin-noticias-capa"></div>
              <div class="imgmin-noticias">
                <?php the_post_thumbnail( ); ?>
              </div>
            </div>
            <div class="bar-noticias large-13 columns">
              <h1 class="title-destaque"><a title="<?php the_title(); ?>" href="<?php echo get_permalink(); ?>">
<?php title_limite(55); ?>

                </a></h1>
              <div class="resumoPost"> 
                <!-- ADICIONA O BLOCO RESUMO DO POST -->
                <?php the_excerpt_max_charlength(180); ?>
              </div>
              <span> <a title="Ver o post completo de: <?php the_title(); ?>" href="<?php echo get_permalink(); ?>" class="plus-destaq"> + </a> </span>
              <span class="post-date"><?php the_time( get_option( 'date_format' ) ); ?></span>
              </div>
          </div>
          <?php endwhile; ?>
          <?php endif; ?>

This code only displays Cat1 , I tried to replicate the code by changing the the_post('category_name=cat1'); but there it gives an error and displays 10 posts 3 times of cat1 . How do I write without conflict? Can you do within that code yourself?

    
asked by anonymous 27.10.2014 / 18:43

2 answers

2

You can make sub loops using WP_Query

<?php while ( have_posts() ) : the_post(); ?>
<?php /* LOOP PRIMARIO */ ?>
<?php endwhile; ?>

<?php
# Vide http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
$args_cat1 = array();
$cat_query = new WP_Query( $args_cat1 );
if ( $cat_query->have_posts() ) :
    while ( $cat_query->have_posts() ) : $cat_query->the_post();
        the_title();
        if ( has_post_thumbnail() ) {
        }
    endwhile;
endif;
    
28.10.2014 / 16:06
0

Look, I did not test it in your way ... but I got it using query_posts(''); and also <?php wp_reset_query(); ?> it looked like this:

<!-- NOTÍCIAS Federadas -->
        <div class="clearfix"></div>
        <div class="large-6 columns un-title">Federadas da AMB</div>
        <div class="clearfix"></div>
             <?php 
          query_posts('cat=10'); //AQUI EU DETERMINEI A CATEGORIA QUE VAI APARECER
          if (have_posts('order=DESC')) : while (have_posts('')) : the_post(''); 
          ?>
          <div class="nto-destaq row columns">
            <div class="imgmin-noticias-box large-3">
              <div class="imgmin-noticias-capa"></div>
              <div class="imgmin-noticias">
                <?php the_post_thumbnail( ); ?>
              </div>
            </div>
            <div class="bar-noticias large-13 columns">
              <h1 class="title-destaque"><a title="<?php the_title(); ?>" href="<?php echo get_permalink(); ?>">
<?php title_limite(55); ?>

                </a></h1>
              <div class="resumoPost"> 
                <!-- ADICIONA O BLOCO RESUMO DO POST -->
                <?php the_excerpt_max_charlength(180); ?>
              </div>
              <span> <a title="Ver o post completo de: <?php the_title(); ?>" href="<?php echo get_permalink(); ?>" class="plus-destaq"> + </a> </span>
              <span class="post-date"><?php the_time( get_option( 'date_format' ) ); ?></span>
              </div>
          </div>
          <?php endwhile; ?>
          <?php endif; ?>
          <?php wp_reset_query(); //AQUI EU RESETO A QUERY QUE ESTAVA ARMAZENANDO A CATEGORIA ?>

<!-- NOTÍCIAS Sociedade de Especialidade -->   
        <div class="clearfix"></div>
        <div class="large-6 columns un-title">Sociedades de Especialidade</div>
        <div class="clearfix"></div>
             <?php 
          query_posts('cat=9');//AQUI EU DETERMINO OUTRA CATEGORIA
          if (have_posts('order=DESC')) : while (have_posts('')) : the_post(''); 
          ?>
          <div class="nto-destaq row columns">
            <div class="imgmin-noticias-box large-3">
              <div class="imgmin-noticias-capa"></div>
              <div class="imgmin-noticias">
                <?php the_post_thumbnail( ); ?>
              </div>
            </div>
            <div class="bar-noticias large-13 columns">
              <h1 class="title-destaque"><a title="<?php the_title(); ?>" href="<?php echo get_permalink(); ?>">
<?php title_limite(55); ?>

                </a></h1>
              <div class="resumoPost"> 
                <!-- ADICIONA O BLOCO RESUMO DO POST -->
                <?php the_excerpt_max_charlength(180); ?>
              </div>
              <span> <a title="Ver o post completo de: <?php the_title(); ?>" href="<?php echo get_permalink(); ?>" class="plus-destaq"> + </a> </span>
              <span class="post-date"><?php the_time( get_option( 'date_format' ) ); ?></span>
              </div>
          </div>
          <?php endwhile; ?>
          <?php endif; ?>
          <?php wp_reset_query(); //AQUI EU RESETO A QUERY QUE ESTAVA ARMAZENANDO A CATEGORIA ?>  
    
28.10.2014 / 17:28