如何以不同的方式设计第一个帖子,并将其他帖子包装在自己的容器中

时间:2020-12-05 作者:user13602245

我需要帮助,以不同的方式设计循环中的第一个项目,并将其余项目包装在一个容器中。我看到了很多似乎相关的问题和它们的各种答案,但我相信我的答案是不同的。

我目前拥有的:

<div class="dpe-flexible-posts row">

<?php if ( $flexible_posts->have_posts() ): $postCount = 0; while ( $flexible_posts->have_posts()): $postCount++; $flexible_posts->the_post(); global $post;
?>

<?php if($postCount == 1) { ?>

    <div <?php post_class(\'col-8\'); ?>>
        <div class="dpe-flexible-posts-inner magB30">
            <div class="post-thumbnail">
                <a href="<?php echo the_permalink(); ?>">
                    <?php
                        if ( $thumbnail == true ) {
                            // If the post has a feature image, show it
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail( $thumbsize );
                            // Else if the post has a mime type that starts with "image/" then show the image directly.
                            } else  { ?>
                                <div class="entry-image">
                                  <a href="<?php the_permalink(); ?>" rel="bookmark">
                                <img src="<?php echo get_stylesheet_directory_uri() . \'/inc/assets/img/default-image.png\'; ?>" alt="<?php the_title(); ?>" />
                                </a>
                                </div>
                            <?php } ?>
                        <?php } ?>
                </a>
            </div>
            <div class="dpe-posts-content">
                <h3 class="h2"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
            </div>
        </div>
    </div>

<?php } else { ?>

    <div <?php post_class(\'col-4\'); ?>>
        <div class="dpe-flexible-posts-inner">
                <div class="post-thumbnail">
                <a href="<?php echo the_permalink(); ?>">
                    <?php
                        if ( $thumbnail == true ) {
                            // If the post has a feature image, show it
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail( $thumbsize );
                            // Else if the post has a mime type that starts with "image/" then show the image directly.
                            } else  { ?>
                                <div class="entry-image">
                                  <a href="<?php the_permalink(); ?>" rel="bookmark">
                                <img src="<?php echo get_stylesheet_directory_uri() . \'/inc/assets/img/default-image.png\'; ?>" alt="<?php the_title(); ?>" />
                                </a>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </a>
                </div>
                <div class="dpe-posts-content">
                    <h3 class="h5"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
                </div>
        </div>
     </div>

<?php } ?>

<?php endwhile; ?>

<?php   
endif; // End have_posts()
使用上述代码,我可以实现以下目标:

< div class="row">

< div class="col-12 col-md-8 first-post">
</div>

< div class="col-12 col-md-4 other-post">
</div>
< div class="col-12 col-md-4 other-post">
</div>
< div class="col-12 col-md-4 other-post">
</div>
< div class="col-12 col-md-4 other-post">
</div>

</div>
但这正是我需要帮助实现的目标:

enter image description here

< div class="first-container">

< div class="first-post">
</div>

</div>

< div class="row second-container">

< div class="col-12 col-md-4 other-post">
</div>
< div class="col-12 col-md-4 other-post">
</div>
< div class="col-12 col-md-4 other-post">
</div>
< div class="col-12 col-md-4 other-post">
</div>

</div>
我希望第一个帖子被包装在自己的容器中,而其余的则被包装在不同的容器中,这样我就可以将垂直旋转木马与swiper js集成在一起。

提前谢谢。

1 个回复
SO网友:Raashid Din

如果您试图使用不同的HTML来设置第一篇文章的样式,而使用其他HTML结构来设置其他文章的样式。那么这段代码可能会有所帮助。

<div class="dpe-flexible-posts row">

<?php 

$firstQuery = new WP_Query(\'showposts=1\');

if ( $firstQuery->have_posts() ) {
    while ( $firstQuery->have_posts() ) {
        $firstQuery->the_post();
        ?>
        <div <?php post_class(\'col-8\'); ?>>
            <div class="dpe-flexible-posts-inner magB30">
                <div class="post-thumbnail">
                    <a href="<?php echo the_permalink(); ?>">
                        <?php
                            if ( $thumbnail == true ) {
                                // If the post has a feature image, show it
                                if ( has_post_thumbnail() ) {
                                    the_post_thumbnail( $thumbsize );
                                // Else if the post has a mime type that starts with "image/" then show the image directly.
                                } else  { ?>
                                    <div class="entry-image">
                                      <a href="<?php the_permalink(); ?>" rel="bookmark">
                                    <img src="<?php echo get_stylesheet_directory_uri() . \'/inc/assets/img/default-image.png\'; ?>" alt="<?php the_title(); ?>" />
                                    </a>
                                    </div>
                                <?php } ?>
                            <?php } ?>
                    </a>
                </div>
                <div class="dpe-posts-content">
                    <h3 class="h2"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
                </div>
            </div>
        </div>
        <?php
    }
}

$secondQuery = new WP_Query(\'offset=1\');
if ( $secondQuery->have_posts() ) {
    while ( $secondQuery->have_posts() ) {
        $secondQuery->the_post();
        ?>
        <div <?php post_class(\'col-4\'); ?>>
            <div class="dpe-flexible-posts-inner">
                    <div class="post-thumbnail">
                    <a href="<?php echo the_permalink(); ?>">
                        <?php
                            if ( $thumbnail == true ) {
                                // If the post has a feature image, show it
                                if ( has_post_thumbnail() ) {
                                    the_post_thumbnail( $thumbsize );
                                // Else if the post has a mime type that starts with "image/" then show the image directly.
                                } else  { ?>
                                    <div class="entry-image">
                                      <a href="<?php the_permalink(); ?>" rel="bookmark">
                                    <img src="<?php echo get_stylesheet_directory_uri() . \'/inc/assets/img/default-image.png\'; ?>" alt="<?php the_title(); ?>" />
                                    </a>
                                    </div>
                                <?php } ?>
                            <?php } ?>
                        </a>
                    </div>
                    <div class="dpe-posts-content">
                        <h3 class="h5"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
                    </div>
            </div>
        </div>
        <?php
    }
}

相关推荐

URL Design for Sub-Posts?

我目前有:mysite.com/product-name mysite.com/another-product 等,其中产品名称和其他产品是帖子。然后,我有一个名为Changelogs的自定义帖子类型,我为每个产品都有这个类型,是否可以有类似以下内容的url:mysite.com/product-name/changelog mysite.com/another-product/changelog 如果是的话,我该怎么做呢?