javascript - 如何在 WordPress 中创建响应式图像轮播和幻灯片自定义帖子类型内容
问题描述
我想创建一个响应式图像轮播和幻灯片自定义帖子类型内容,如特色图像、标题等。
我通过以下方式创建了自定义帖子类型作为横幅:
function create_banners_post_type() {
$labels = array(
'name' => __( 'Banners' ),
'singular_name' => __( 'banner' ),
'add_new' => __( 'New banner' ),
'add_new_item' => __( 'Add New banner' ),
'edit_item' => __( 'Edit banner' ),
'new_item' => __( 'New banner' ),
'view_item' => __( 'View banner' ),
'search_items' => __( 'Search banners' ),
'not_found' => __( 'No banners Found' ),
'not_found_in_trash' => __( 'No banners found in Trash' ),
);
$args = array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'custom-fields',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'post_tag', 'category'),
);
register_post_type( 'banner', $args );
}
add_action( 'init', 'create_banners_post_type' );
并在首页显示输出:
// function to show home page banner using a query of banner post type
function home_page_banner() {?>
<?php
$query = new WP_Query( array(
'post_type' => 'banner',
));
if ( $query->have_posts() ) { ?>
<ul>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<div>
<div id="post-<?php the_ID(); ?>" <?php post_class( 'bg-image' ); ?>><?php the_post_thumbnail(); ?></div>
<div class="content-wrap">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<a href="#section-b" class="btn">Read More</a>
</div>
</div>
</li>
<?php
endwhile;?>
</ul>
</div>
<?php
}
wp_reset_postdata();
}
在首页我插入了这段代码:
<header id="showcase" class="grid">
<?php
if ( is_front_page() ) {
home_page_banner();
}
?>
</header>
解决方案
我以前从未使用过 Owl Carousel,但任何时候我必须做一个旋转木马,我都会去 Slick 或 Flickity - 基本上拥有你可能需要的一切。
以下是如何将您的帖子内容包含在轮播中的示例。
创建初始查询:
<?php
// the query
$recent_posts = new WP_Query( array(
'category_name' => 'posts',
'posts_per_page' => 3,
));
?>
创建你的滑块
<div class="posts-slider"> <? // this is your wrapper div ?>
<?php if ( $recent_posts->have_posts() ) : ?>
<?php while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<div class="recent-post-slide">
<div class="recent-post-content">
<div class="row">
<div class="col-12">
<div>
<div id="post-<?php the_ID(); ?>" <?php post_class( 'bg-image' ); ?>><?php the_post_thumbnail(); ?></div>
<div class="content-wrap">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<a href="#section-b" class="btn">Read More</a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
添加一些 jQuery 以使 Flickity 工作:
<script>
jQuery(document).ready(function() {
var slider = jQuery('.posts-slider'); // The class name of the wrapper div
slider.flickity({
cellAlign: 'left',
contain: true
});
});
</script>
推荐阅读
- apache-kafka - 什么是卡夫卡经纪人?
- c++ - 有没有办法同时使用 C 和 C++?
- amazon-web-services - 无法通过 HTTP 连接到 EC2
- reactjs - 无法在 onclick 事件处理程序中传递参数 - React
- sql-server - Azure 上的 .NET Core 无法连接到 SQL Server 数据库
- html - CSS:如何使 div 背景宽度适合
- php - 根据用户定义的索引顺序对数组进行排序
- json - 如何将数据保存回原始 json 文件?
- node.js - 无法读取未定义 nodejs 的属性“forEach”
- set - 如何找到集合中的元素个数?CPLEX OPL