使用分页和帖子数量选项构建内容和摘录网格循环

时间:2012-10-29 作者:markratledge

我想做的是:在索引上。php,一个循环,使用the_content 然后在下面显示一个可选的摘录数量(帖子数量可以在函数中硬编码)(使用the_excerpt) 在一个网格模式中,2个摘录宽。任何paged 文章页面仅使用摘录的网格显示。像这样:

index post display

paged post display

如果该循环处理的是文本、摘录和内容,而不是图像,则该循环将非常理想:http://www.billerickson.net/a-better-and-easier-grid-loop/

Yet Another Update 11/03/12:

感谢凯撒。最后一个错误:第二页显示了2篇完整的文章和摘录,而不是所有的摘录。

    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 );

2 个回复
SO网友:kaiser

The$wp_query 属性允许“很多”

实际上,如果使用$wp_query 类似对象current_post.

在这里,您可以看到一些示例,它们巧妙地使用了以下内容is_paged(), $wp_query->current_post$wp_query->posts_per_page. 您可以切换标记,这取决于您是在第一页还是更高的页面上,是在前三页(或任何数字)还是更高的帖子上。使用post_class() 函数,该函数还有一个名为post_class() - 它有三个参数:$classes (默认WP核心类)$class (调用时定义的类数组-请参见下面的示例)和$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 );

SO网友:Marco

进行以下更改。未经测试:

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();
}
?>

结束