【问题标题】:Integration of Bootstrap 4 Carousel in Wordpress在 Wordpress 中集成 Bootstrap 4 Carousel
【发布时间】:2019-07-14 10:22:51
【问题描述】:

我目前正在为我的博客开发一个主题,我有一个关于在 Wordpress 中集成 Carousel Slider 的问题。

滑块效果很好,显示最新的 5 篇帖子,带有特色图片和带有帖子标题的标题。但是我不得不为img设置一个固定的高度,因为不同大小的图像会不断改变轮播的高度,所以结果不是很好。

问题是在移动设备上img拉伸很多,所以我想将帖子缩略图设置为轮播的背景,这样我可以设置bg-size,bg-position等以实现完美响应式图片。

你知道有没有办法达到这个目标?或者也许我的实际代码可以在不将缩略图设置为背景的情况下使用?

提前谢谢大家。

<div class="carousel-inner">
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
           <?php $active_class = ( 0 === $the_query->current_post ) ? ' active': ''; ?>
                <div class="carousel-item <?php echo esc_attr( $active_class ); ?>">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                    <?php the_post_thumbnail('full') ?>
                    </a>
                    <div class="carousel-caption d-md-block">
                        <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
                    </div>
                </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
</div>

【问题讨论】:

    标签: wordpress twitter-bootstrap


    【解决方案1】:

    您可以使用add_image_size( 'home-carousel', &lt;width here&gt;, &lt;height here&gt;, false ); 为您的轮播注册新图片。

    然后,您可以像这样调用 the_post_thumbnail('full') - the_post_thumbnail('home-carousel')。如果您已经上传了图片,则需要使用此plugin 重新生成缩略图。

    如果您仍想使用背景图片选项 - 您可以通过此函数获取特色图片的 URL - get_the_post_thumbnail_url 并将其设置为 .carousel-item div 的背景。

    【讨论】:

    • 非常感谢。我将使用背景图像选项,似乎对我的目的更敏感..但是,我怎样才能添加永久链接?我的意思是,使用您可以在第一篇文章中看到的“旧”代码,当我点击缩略图时,轮播会将我重定向到帖子本身.. 但显然,使用背景选项这不起作用.. 也许这既愚蠢又容易,但我现在被困住了。
    【解决方案2】:

    这是我使用缩略图作为背景图像的实际代码。但是,就像我说的,固定链接不起作用。有没有办法让它以缩略图为背景工作?如果我不清楚,我希望当用户单击图像时,它会重定向到单页帖子。

    <div class="carousel-item <?php echo esc_attr( $active_class ); ?>" style="background:url('<?php the_post_thumbnail_url('full');?>') center center no-repeat; background-size: cover; min-height: 75vh;">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            </a>
            <div class="carousel-caption d-md-block">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
            </div>
    </div>
    

    【讨论】:

    • 只需将 div.carousel-item 替换为 a.carousel-item 并添加 href 属性。此外,删除轮播项目内的空链接。这应该有效。
    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 2018-07-16
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2022-11-19
    相关资源
    最近更新 更多