dongyong6428 2019-07-04 17:13
浏览 75

如何在两列/ Wordpress中进行post循环

I've been trying to split posts for a long time. I tried a variety of variations, but every time I post a single post or copy me all double. I mean multiple posts.

If anyone has an idea how to fix it I will be very grateful to him.

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php do_action( 'esteem_before_post_content' ); ?>
    <?php
        if( has_post_thumbnail() ) {
            $image = '';
            $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'blog-large');
            $title_attribute = the_title_attribute( 'echo=0' );
            $image .= '<figure class="post-featured-image">';
            $image .= '<a href="' . get_permalink() . '" title="'.the_title_attribute( 'echo=0' ).'">';
            $image .= get_the_post_thumbnail( $post->ID, 'blog-large', array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ) ) ).'</a>';
            $image .= '<div class="mask">
                        <div class="image-icon-wrap">
                            <a href="'.$large_image_url[0].'" class="img-icon img-search"><i class="icon-search"></i></a>
                            <a href="'.get_permalink().'" class="img-icon img-link"><i class="icon-link"></i></a>
                        </div>
                    </div>';
            $image .= '</figure>';

            echo $image;
        }
    ?>
    <div class="blog-content">
        <header class="entry-header">
            <h2 class="entry-title">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();?>"><?php the_title(); ?></a>
            </h2><!-- .entry-title -->
        </header>

        <?php esteem_entry_meta(); ?>

        <div class="entry-content clearfix">
            <?php the_excerpt(); ?>

        </div><!-- .entry-content -->
    </div>

    <?php do_action( 'esteem_after_post_content' ); ?>
</article>

Post loop

<div id="primary">
    <div id="content" class="clearfix">

        <?php if ( have_posts() ) : ?>

            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', get_post_format() ); ?>

            <?php endwhile; ?>

            <?php get_template_part( 'navigation', 'none' ); ?>

        <?php else : ?>

            <?php get_template_part( 'no-results', 'none' ); ?>

        <?php endif; ?>

    </div><!-- #content -->
</div><!-- #primary -->

If anyone has an idea how to fix it I will be very grateful to him.

  • 写回答

1条回答 默认 最新

  • dousi2029 2019-07-04 18:34
    关注

    Since you are already calling post_class() function, you need to pass alternate class to that function itself and make sure to do this only on your custom page or selected blog page. The code will essentially be something like:

    global $current_class;
    $current_class = 'odd'; 
    function alternating_post_class ( $classes ) {
        global $current_class;
        if( is_page(<PAGE_ID_HERE>) || is_home() ):
            $classes[] = $current_class;
            $current_class = ($current_class == 'odd') ? 'even' : 'odd';
        endif;
        return $classes;
    }
    add_filter ('post_class', 'alternating_post_class');
    

    This should resolve the problem by giving you odd/even class which you can use to style your two columns.

    Cheers Mate!!!

    评论

报告相同问题?