Forums

Show post of a category if exist. Else show post of other category (2 posts)

  1. MZBS
    Member
    Posted 1 year ago #

    Hello, i have this problem:

    In the index of my web i have an area which gets the first post from one category and the second post from other category

    <?php $reciente = new WP_Query("cat=7&showposts=1");
    
      while($reciente->have_posts()) : $reciente->the_post();?>
    
      <p class="newshome"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>   <p class="fechahome"><?php the_time('j/m/Y'); ?></p>
      <div class="entryhome">
    						<?php the_excerpt(''); ?>
    
    					</div>
      <?php endwhile; ?>

    and

    <?php $reciente = new WP_Query("cat=6&showposts=1");
    
      while($reciente->have_posts()) : $reciente->the_post();?>
    
      <p class="newshome"><a>"><?php the_title(); ?></a></p>   <p class="fechahome"><?php the_time('j/m/Y'); ?></p>
      <div class="entryhome">
    						<?php the_excerpt(''); ?>
    
    					</div>
      <?php endwhile; ?>

    I want to put that if there is no post on category 6 get last post of category 1. And if there is no post in category 7 get last post of category 1 but no the same post I have in the first area. I mean i have 2 posts there and if there is no post in cat 6 and 7 i want to get the last 2 post of category 1.

    How can I do it?

    Thanks!

  2. MZBS
    Member
    Posted 1 year ago #

    Now I have this:

    <?php $reciente = new WP_Query("cat=6&showposts=1");
    
      if($reciente->have_posts()) : $reciente->the_post();?>
    
      <p class="newshome"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>   <p class="fechahome"><?php the_time('j/m/Y'); ?></p>
      <div class="entryhome">
    						<?php the_excerpt(''); ?>
    
    					</div>
      <?php endif; ?>
    
      <?php $reciente = new WP_Query("cat=3&showposts=1");
      while($reciente->have_posts()) : $reciente->the_post();?>
      <p class="newshome"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>   <p class="fechahome"><?php the_time('j/m/Y'); ?></p>
      <div class="entryhome">
    						<?php the_excerpt(''); ?>
    
    					</div>
      <?php endwhile; ?>
    
              <?php $reciente2 = new WP_Query("cat=7&showposts=1");
    
      if($reciente2->have_posts()) : $reciente2->the_post();?>
    
      <p class="newshome"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>   <p class="fechahome"><?php the_time('j/m/Y'); ?></p>
      <div class="entryhome">
    						<?php the_excerpt(''); ?>
    
    					</div>
      <?php endif; ?>
      <?php $reciente2 = new WP_Query("cat=3&showposts=1");
      while($reciente2->have_posts()) : $reciente2->the_post();?>
      <p class="newshome"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>   <p class="fechahome"><?php the_time('j/m/Y'); ?></p>
      <div class="entryhome">
    						<?php the_excerpt(''); ?>
    
    					</div>
      <?php endwhile; ?>

    So if there is no post in cat 6 or 7 i get the last posts of the general cat 3. Correct. But the problem is that if there is no post in the cat. 6 and also in cat 7. i get the same last post in the first and the second area.

    So i need a new conditional that check if in the first area is the last post of the cat 3, so in the second area should get the penultimate post.

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic