Diversify Wordpress posts by Categories

Asked

Viewed 65 times

2

I’m using this code to display the news on the home page of a website, only I wanted to display:

4 news from Cat1 + 3 of Cat2 + 3 of 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 the Cat1, I tried to replicate the code by changing the the_post('category_name=cat1'); but then gives an error and displays 10 posts 3 times from cat1. How do I write without conflict? You can do it within the same code?

2 answers

2

You can make secondary 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;

0

Look, I didn’t test it your way... but I did it using query_posts(''); and also <?php wp_reset_query(); ?> there it was 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 ?>  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.