• Resolved dr06u

    (@dr06u)


    Hello,

    Is there any way to accomplish this in index.php

    (I’m writing the output not the php)

    <div id="container">
        <div id=firstpost"></div>
        <div id="secondpost"></div>
    ...
        <div id="last-posts-container">
         <div id="ninth"></div>
         <div id="tenth"></div>
        </div> <!-- End of last posts container --!>
    </div>

    I want for example latest 5 posts to have a style (same style all latest 5 of them) and last 5 posts to have another style (same style all 5 of them). And I want the last 5 posts to be wrapped inside a div. I managed to accomplish the first part (to make the last five different from the latest 5) but I can’t seem to understand how can I set a div outside the loop in other to wrap around the last 5 posts.

    I’m using this method (someone posted it on this forum ) ,now, but I’m open to any other method.

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $c++;
    if( !$paged && $c == 1 or $c == 2 or $c == 3 or $c == 4 or $c == 5) :?>
    latest 5 posts html code
    <?php else :?>
    last 5 posts html code
    <?php endif;?>
    <?php endwhile; ?>
    <?php endif;?>

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • sreedhanya

    (@sreedhanya)

    Hai,
    First section of your template display latest five posts
    <?php $my_query = new WP_Query(‘showposts=5’); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h4>” title=”<?php the_title(); ?>”><?php the_title(); ?></h4>
    <?php the_content(” More…”,TRUE,”); ?>
    <?php endwhile; ?>

    second section of your template display old five posts
    <?php $my_query = new WP_Query(‘showposts=5&offset=5’); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h4>” title=”<?php the_title(); ?>”><?php the_title(); ?></h4>
    <?php the_content(” More…”,TRUE,”); ?>
    <?php endwhile; ?>

    Thank you

    Thread Starter dr06u

    (@dr06u)

    Thank you a lot !!!!!! Works perfect!

    Thread Starter dr06u

    (@dr06u)

    One more question :
    How do I make pagination now? I was having

    <?php
    global $wp_query;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
    	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format' => '?paged=%#%',
    	'prev_text' => __('? Previous'),
        'next_text' => __('Next ?'),
    	'current' => max( 1, get_query_var('paged') ),
    	'total' => $wp_query->max_num_pages
    ) );
    ?>

    But this doesn’t work anymore with this loop

    [ Please do not bump, that’s not permitted here. ]

    Thread Starter dr06u

    (@dr06u)

    I managed to partially fix pagination by adding code from this post

    https://www.ads-software.com/support/topic/plugin-wp-pagenavi-wp_pagenavi-does-not-work-with-offset?replies=17

    Like this :

    <?php $my_query = new WP_Query('showposts=5'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h4>" title="<?php the_title(); ?>"><?php the_title(); ?></h4>
    <?php the_content(" More...",TRUE,''); ?>
    <?php endwhile; ?>
    
    second section of your template display old five posts
    <?php
    $last_five = get_posts('numberposts=5');
    $last_five_ids = array();
    if( $last_five ) foreach( $last_five as $last_post ) { $last_five_ids[] = $last_post->ID; }
    $my_query = new WP_Query(array('post__not_in' => $last_five_ids, 'posts_per_page' => 5, 'paged' => get_query_var('paged') ) );  ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h4>" title="<?php the_title(); ?>"><?php the_title(); ?></h4>
    <?php the_content(" More...",TRUE,''); ?>
    <?php endwhile; ?>

    Now I need a solution to make the first loop skip every 5 next posts. Because the problem is that the first loop displays on the next page the 5 posts from the second loop of the page before(the first page), while the second loop displays on the next page the 5 posts that the first loop should have displayed.

    In other words I have , for example, 20 posts, instead of having 2 pages with 5 posts in the first loop and 5 in the second I have 3 pages :
    First page: 5 posts in the first loop and 5 in the second
    Second page: 5 posts (the 5 posts from the first’s page second loop) in the first loop and 5 posts in the second loop (the posts that should be in the first loop of this page *second page)
    -…and so on…
    Thank you.

    Thread Starter dr06u

    (@dr06u)

    I finally solved it like this:

    <div id="first-loop-container">
    <?php if (have_posts()) : ?>
    <?php $count = 0; ?>
    
    <?php while (have_posts()) : the_post(); ?>
    <?php $count++; ?>
    
    <?php if ($count <= 5) : ?>
    <h4 title="<?php the_title(); ?>"><?php the_title(); ?></h4>
            <?php the_content(" More...",TRUE,'');
        <?php endif; endwhile; ?>
    </div>
    
    <div id="second-loop-container">
    <?php rewind_posts();
    $cnt = 0;
    while (have_posts()) : the_post(); if ($cnt >= 5 ): ?>
    <h4 title="<?php the_title(); ?>"><?php the_title(); ?></h4>
            <?php the_content(" More...",TRUE,'');
    <?php endif; $cnt++; endwhile; ?>
    
    </div>
    
    <?php
    global $wp_query;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'prev_text' => __('&laquo; Inapoi'),
        'next_text' => __('Urmatoarea &raquo;'),
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
    ?>
    <?php else : ?>
    <?php endif; ?>

    Thank again for you answer sreedhanya.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Last n (2/3/4 etc.) posts in a separate container’ is closed to new replies.