WordPress.org

Ready to get started?Download WordPress

Forums

Display latest post in two categories with individual next/previous (3 posts)

  1. titusbartos
    Member
    Posted 2 years ago #

    This is the index.php of a website I am working on.

    Basically what I need is to have two separate loops showing only the latest post in two separate categories (5 and 1), but when the viewer clicks on previous in one category, the other category still displays the same post it did to begin with.

    So far I haven't been able to figure out how to do this with showing just one post per category.

    Would anyone be able to help me? Many thanks in advance!

    <?php get_header(); ?>
    <div id="container">							
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>	
    
    <div id="reading-plan">
    <?php if ( in_category('5') ) { ?>
    <?php the_content('Continue reading ยป'); ?>
    <?php } ;?>
    </div>				
    
    <div id="content" role="main">
    <div id="contentInner">
    <div id="post">
    
    <?php if ( in_category('1') ) { ?>
    <div class="entry-content">
    <?php the_content'Read more...'); ?>
    </div><!-- .entry-content -->
    <?php } ;?>
    
    </div><!-- #post-## -->
    </div><!-- #contentInner-## -->
    
    </div><!-- #content -->
    <?php endwhile; else: ?>
    <?php endif; ?>
    </div><!-- #container -->
    
    <?php get_footer(); ?>
  2. keesiemeijer
    moderator
    Posted 2 years ago #

    The code shown is a single loop. If you want multiple loops I think only one can be paginated.

    but maybe try with something like this (not tested example code):

    <!-- first loop -->
    <?php $the_query = new WP_Query( 'posts_per_page=1&cat=5' );   ?>
    <?php if (have_posts()) : ?>
      <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                   <!-- do stuff ... -->
    <?php next_post_link('%link', 'Next post in category', TRUE); ?>
    <?php previous_post_link('%link', 'Previous in category', TRUE); ?>
    <?php endwhile; endif; ?>
    
    <!-- second loop -->
    <?php $the_query = new WP_Query( 'posts_per_page=1&cat=1' );   ?>
    <?php if (have_posts()) : ?>
    while ( $the_query->have_posts() ) : $the_query->the_post();
                   <!-- do stuff ... -->
    <?php next_post_link('%link', 'Next post in category', TRUE); ?>
    <?php previous_post_link('%link', 'Previous in category', TRUE); ?>
    <?php endwhile; endif; ?>
  3. titusbartos
    Member
    Posted 2 years ago #

    Thanks for the reply, k,
    I tried the code, formatted it around a bit, not much luck. Both divs display posts, but the posts repeat themselves when I click on previous.

Topic Closed

This topic has been closed to new replies.

About this Topic