4

What I'd like to do: on index.php, a loop that shows a selectable number of full posts using the_content and then below that shows a selectable number (the number of posts can be hardcoded in the function) of excerpts (using the_excerpt) in a grid pattern 2 excerpts wide. Any paged page of posts only uses the grid display of excerpts. Like this:

index post display

paged post display

This loop would be ideal if it handled text and excerpts and content rather than images: http://www.billerickson.net/a-better-and-easier-grid-loop/

Yet Another Update 11/03/12:

Thanks to kaiser for this. One last bug: the second page shows the 2 full posts and excerpts rather than all excerpts.

    global $wp_query;
    if ( have_posts() )
    {
        while( have_posts() )
        {
            the_post();

            // Add Class: "post-number-X"
            $current_post = "post-number-{$wp_query->current_post}";

            // Add Class: "home" (for index page) or "post-number-X-of-total";
            $current_in_total = 'home';
            if ( is_paged() )
            {

                $current_in_total  = "post-number-";
                $current_in_total .= get_query_var( 'paged' ) * get_query_var( 'posts_per_page' ) - $wp_query->current_post;
                $current_in_total .= "-of-total";
            }

            // Add Class: Even/Odd
            $even_odd = ( 0 === $GLOBALS['wp_query']->current_post % 2 ) ? ' even' : ' odd';
            // Avoid even/odd class for excerpts (everything after the 2nd post)
            2 > $wp_query->current_post AND $even_odd = '';

            // MarkUp: Uses `post_class()` to add classes
            ?>
     <article <?php post_class( "{$current_post}{$even_odd} {$current_in_total}" ); ?>>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php 
                // Home/Index/Front-Page/Archive first page
                if ( ! is_paged() )
                {
                    // Display the_content(); for the first 2 posts, then the_excerpt();
                    2<= $wp_query->current_post ? the_excerpt() : the_content();
                }
                // Paged archives (starts at second page)
                else
                {
                    the_excerpt();
                }
                ?>
            </article>
            <?php
        } 

// Add navigation ...
    twentyeleven_content_nav( 'nav-below' );

    } // endif;
    unset( $current_post, $current_in_total, $even_odd );
5
  • 1
    I have to differ on your suggestion that "it's proprietary code", all code that interfaces with WordPress functionality is subject to GPL. I think you should try and re-word the question to zone into a possible solution for offset paging.
    – Wyck
    Commented Oct 29, 2012 at 6:02
  • You're right; last I had heard (not recently) there was a difference in opinion between the thme developer and WP, but I see this: mashable.com/2010/07/22/thesis-relents Commented Oct 29, 2012 at 13:23
  • 1
    A sidenote: I have a license of »Thesis« and had really hard times trying to develop with it (and not against it). It's imho pretty crappy coded and threw tons of warnings/errors and notices. Impossible to catch your own errors in between those. My suggestion: Get another parent theme. Genesis is pretty decent as well as Hybrid.
    – kaiser
    Commented Oct 29, 2012 at 13:46
  • 1
    Ya I was going to say the above example is weird code to store even and odd values when numbers exists. My edit suggestion was to remove the theme shop's name because it's a good question irrespective of the theme, especially in this case..
    – Wyck
    Commented Oct 29, 2012 at 14:11
  • The code snip above that alternates classes is from a different theme, not sure where. I did think of moving my whole theme (which is based on twentyeleven, but is not a parent/child) into Genesis for ease of use and to use that grid loop of billericksons's, but everything else is finished except for this main loop I want. Commented Oct 29, 2012 at 14:34

2 Answers 2

2

The $wp_query properties allow "alot"

Actually it's not that hard if you use parts of the $wp_query object like current_post.

Here you can see some examples that make some tricky use of things like is_paged(), $wp_query->current_post and $wp_query->posts_per_page. You can switch MarkUp depending on if you're on the first or later pages, if you got the first three (or whatever number) or later posts. It's also nice to use the post_class() function, which also has a filter named post_class() - it has three arguments: The $classes (default WP core classes), the $class (an array of classes you defined while calling it - see below example) and the $post_ID.

global $wp_query;
if ( have_posts() )
{

    // Add navigation ... TOP
    twentyeleven_content_nav( 'nav-above' );

    while( have_posts() )
    {
        the_post();

        // Add Class: "post-number-X"
        $current_post = "post-number-{$wp_query->current_post}";

        // Add Class: "home" (for index page) or "post-number-X-of-total";
        $current_in_total = 'home';
        if ( is_paged() )
        {

            $current_in_total  = "post-number-";
            $current_in_total .= get_query_var( 'paged' ) * get_query_var( 'posts_per_page' ) - $wp_query->current_post;
            $current_in_total .= "-of-total";
        }

        // Add Class: Even/Odd
        $even_odd = ( 0 === $GLOBALS['wp_query']->current_post % 2 ) ? ' even' : ' odd';
        // Avoid even/odd class for excerpts (everything after the 3rd post)
        3 > $wp_query->current_post AND $even_odd = '';

        // MarkUp: Uses `post_class()` to add classes
        ?>
        <article <?php post_class( "{$current_post}{$even_odd} {$current_in_total}" ); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php 
            // Home/Index/Front-Page/Archive first page
            if ( ! is_paged() )
            {
                // Display the_content(); for the first 3 posts, then the_excerpt();
                3 <= $wp_query->current_post ? the_excerpt() : the_content();
            }
            // Paged archives (starts at second page)
            else
            {
                the_excerpt();
            }
            ?>
        </article>
        <?php
    }

    // Add navigation ... BELOW
    twentyeleven_content_nav( 'nav-below' );

} // endif;
unset( $current_post, $current_in_total, $even_odd );
11
  • Thanks, let me look at this. You use post classes like this example billerickson.net/a-better-and-easier-grid-loop except you're incorporating excerpts and content to handle text, unlike images as billerickson's example. Commented Oct 29, 2012 at 13:31
  • Well, I get a parse error on the line ` $current_in_total = "post-number-" $current_in_total .= get_query_var( 'paged' ) * get_query_var( 'posts_per_page' ) - $wp_query->current_post; $current_in_total .= "-of-total";` And this also adds the odd-even classes to the full posts, which causes them to be displayed in columns instead of full width. And also, where does the paging call <?php twentyeleven_content_nav( 'nav-below' ); ?> ` go? Commented Nov 1, 2012 at 3:34
  • 1
    This definitely looks like the best place to start. What parse error are you getting? For the even/odd, you probably just need to add a $GLOBALS['wp_query']->current_post > $offset (where $offset is your defined offset for full/excerpt posts) to the $even_odd ternary expression conditional. Commented Nov 2, 2012 at 1:21
  • No more description given than parse error in the Add Class: "home"... block. Where exactly do I add that global? Commented Nov 2, 2012 at 3:47
  • @songdogtech The parse error is fixed. There was a missing ; at the end of the first line (You maybe want to get a decent IDE like Eclipse or NetBeans that help you spotting such). If you don't want a line - like even/odd - simply comment it out or remove it. Sry, but I was expecting such knowledge as I saw your reputation. Therefore I thought there's not much explanation needed aside from a example that covers most of what you can do :)
    – kaiser
    Commented Nov 2, 2012 at 12:13
0

Make the following changes. Untested:

global $wp_query, $paged;

parse_str( $query_string, $query_args );   


// Home/Index/Front-Page/Archive first page

if ( 0 == $paged )

{
    // Display the_content(); for the first 2 posts, then the_excerpt();
    2<= $wp_query->current_post ? the_excerpt() : the_content();
}
// Paged archives (starts at second page)
else
{
    the_excerpt();
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.