使用引导程序和 PHP 创建上一篇 WordPress 帖子的滑块

古斯塔沃·马查多

我想创建一个 wordpess 上一篇文章的滑块,它使用引导轮播和附加的 php 代码一一显示

目前代码显示了最后三个帖子,但我希望每张幻灯片上都出现一个

  <?php
 global $post;

$last_posts = get_posts(array('posts_per_page' => 3));

foreach ( $last_posts as $post ) :
 setup_postdata( $post );?>

<div class="last-posts-wrap">
 <?php the_post_thumbnail();?>
 <h3><?php the_title();?></h3>
 <p><?php the_excerpt();?></p>
</div>

<?php endforeach;
 wp_reset_postdata();
?>

    <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src="..." alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="..." alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="..." alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
尼兰巴·夏尔马

从这个问题来看,我猜你正试图从最新的帖子中创建轮播。请检查以下示例。获取 3 个帖子以呈现轮播。active为第一张幻灯片添加了类。这可能会有所帮助。

<?php
$qargs = array(
    'posts_per_page'      => 3,
    'no_found_rows'       => true,
    'ignore_sticky_posts' => true,
    );
$the_query = new WP_Query( $qargs );
?>
<?php if ( $the_query->have_posts() ) : ?>
    <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
        <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 ); ?>">
                    <?php the_post_thumbnail( 'large' ); ?>
                    <h3><?php the_title();?></h3>
                    <p><?php the_excerpt();?></p>
                </div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </div><!-- .carousel-inner -->
        <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
<?php endif; ?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP - Wordpress 上一篇文章链接显示在最后一篇文章中

Wordpress 下一篇上一篇

使用PHP include创建一个简码以用于WordPress帖子和页面

如何使用PHP为WordPress帖子创建重定向?

PHP开关和AJAX显示WordPress帖子

在循环内按帖子类型和ID获取一篇帖子

使用PHP在Wordpress帖子中编写简码

Wordpress 帖子使用引导程序以锯齿形格式显示

WordPress的自定义帖子查询滑块

如何以编程方式从自定义PHP应用程序创建WordPress帖子?

如何在Wordpress上使用php将图像设置为帖子的特色图像?

下一篇和上一篇文章以相同的术语链接自定义分类法

下一个上一篇文章在WordPress中

使用引导程序,ajax和外部php在一页上使用多种形式

关于 PHP 中滑块最近帖子的问题

WordPress,帖子被显示在首页上(index.php)?

Wordpress-使用JS(而非PHP)显示随机帖子

如何使用php在wordpress帖子中获取图像?

循环使用PHP和WordPress按类别组织和显示自定义帖子

Wordpress 只显示一篇文章

显示wordpress的第一篇文章

WP_Query仅返回一篇帖子

WordPress上的JSON API-创建帖子

ACF pro - 创建帖子时的前端表单和 Wordpress 类别

帖子和子站点上的 WordPress 多站点 404 错误

PHP邮件程序无法与Wordpress 5.5一起使用

在Wordpress PHP中输出单个帖子标签

WordPress PHP获得每个帖子的永久链接

Wordpress-使用“ index.php”中“ header.php”中的变量从查询中排除帖子