php - 如何在两列/WordPress中制作帖子循环

标签 php css wordpress

我已经尝试拆分帖子很长时间了。我尝试了多种变体,但每次我发布一个帖子或将我全部复制一倍。 我的意思是多个帖子。

如果有人知道如何修复它,我将非常感谢他。

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

后循环

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

如果有人知道如何修复它,我将非常感谢他。

最佳答案

因为您已经在调用 post_class() 函数,您需要将备用类传递给该函数本身,并确保只在您的自定义页面或选定的博客页面上执行此操作。代码基本上是这样的:

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

这应该可以通过为您提供 odd/even 类来解决问题,您可以使用它来设置两列的样式。

干杯伙计!!!

关于php - 如何在两列/WordPress中制作帖子循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56892132/

相关文章:

jquery - 为什么 noscript 和 skel 在 wordpress 中不起作用?

html - 如何放置我的页脚?

html - Wordpress 在 header 中的动态 css 样式标签中输出多个 body css 样式

css - 在wordpress页面上隐藏插件

mysql - 替换 HTTP ://to HTTPS://in WordPress

javascript - imacros 复制文本列表并在 wordpress 上使用复选框粘贴

php - C++和PHP之间的文件锁定

php - 如何增加 docker 中的超时时间?

javascript - 提交后,确认消息将粘贴在联系表单的底部

php - 将谷歌地图 v2 转换为谷歌地图 v3