WordPress主题使用jQuery插件Isotope添加作品集筛选功能
上一篇文章我们介绍了Isotope,功能强大,效果出众的jQuery插件,但是文章中的代码并不能使用在项目中,所以今天通过Wordpress主题来演示此功能,完全可以使用在你的主题当中。
引入Isotope代码
首先得确保你的主题加载了jQuery插件,然后在Isotope官网下载isotope.pkgd.min.js文件,放在主题的js文件夹下,添加以下代码在footer.php文件夹中:
- <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/isotope.pkgd.min.js"></script>
- < script >
- var $container = $(".portfolio ul").isotope({});
- $("#filters").on("click", "button",
- function() {
- var filterValue = $(this).attr("data-filter");
- $container.isotope({
- filter: filterValue
- })
- });
- $("#filters button").click(function() {
- $("#filters button").removeClass("active");
- $(this).addClass("active")
- });
- < /script>
.portfolio ul为包裹作品列表的元素,#filters为包裹按钮的元素,button为按钮。
作品集页面模板
新建一个名称为template-portfolio.php作品集页面模板,顶部代码为:
添加PHP代码
将以下代码添加到template-portfolio.php文件中,具体位置根据主题代码而定。
- <section class="button-group">
- <div id="filters">
- <?php $terms=get_terms( "portfolio_field"); $count=count($terms); echo '<button class="button active" data-filter="*">'.__( '全部', 'salong' ). '</button>'; if ( $count>0 ){ foreach ( $terms as $term ) { $termname = strtolower($term->name); $termname = str_replace(' ', '-', $termname); echo '
- <button class="button" data-filter=".'.$termname.'">'.$term->name.'</button>'; } } ?>
- </div>
- </section>
- <section class="portfolio portfolio-list">
- <ul>
- <?php $args=array( 'post_type'=>'portfolio', 'posts_per_page' => -1 ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); $terms = get_the_terms( $post->ID, 'portfolio_field' ); if ( $terms && ! is_wp_error( $terms ) ) : $links = array(); foreach ( $terms as $term ) { $links[] = $term->name; } $tax_links = join( " ", str_replace(' ', '-', $links)); $tax = strtolower($tax_links); else : $tax = ''; endif; ?>
- <li class="<?php echo $tax; ?>">
- <article class="portfolio-item">
- <div class="portfolio-img">
- <?php post_thumbnail(); ?>
- </div>
- <div class="portfolio-info">
- <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
- </div>
- </article>
- </li>
- <?php endwhile; ?>
- </ul>
- </section>
这里获取的文章类型为“portfolio”,获取的是全部作品。
[musicbox]在后台新建一个作品集页面,模板选择作品集,就可以实现与萨龙网络作品集一样的筛选效果。[/musicbox]本文原创,作者:萨龙龙,其版权均为萨龙网络所有。
如需转载,请注明出处:https://salongweb.com/wordpress-theme-isotope.html
一个平平常常的人,热爱生活、旅行和摄影,骑行去过西藏,一直计划再去。14年从江西骑行来到大理,现栖居于洱海边,食人间烟火,过简单生活,做简约设计!在开发MNews主题的快讯功能时,希望一天内发布的快讯文章在当天日期下,而不是每篇都显示一个日期,通过the_date()函数可完美的达到这…最近在开发Perimg图片主题,用户想要用Timthumb.php插件来实现缩略图,缩略图的顺序是自定义域、文章第一张图片与默认缩略图,而萨龙龙…WooCommerce商城首页默认被插件转换成归档页面archive-product.php,但是添加的依然是页面,所以通过get_post_m…最近开发了一款多功能的新闻视频类Wordpress主题Yewan,客户的网站用户也比较多,要求设计一个按权限(管理员、编辑、作者、投稿者、订阅者…在很多网页上,尤其是一些科技企业的产品展示页面,当你滚动页面的时候会看到各式各样的元素动画效果,非常的炫丽,很吸引人。WOW.js就是一款帮助你…萨龙网络在2018年9月份就写了一篇《WordPress简约实用的主题选项框架CodestarFramework》文章来介绍Codest…WooCommerce后台对管理员的通知『连接您的店铺到WooCommerce.com接受扩展更新和支持』,是3.3.1版本后出来了,新的…Wordpress获取分类文章的函数有query_posts和WP_Query,两个函数的功能都很强大,推荐使用WP_Query函数来查询文章。…WordPress本身一直有WP-Cron(计划任务)的功能,可以设置每隔一段时间来执行,不过WP-Cron功能是基于页面浏览的,所以时…为了提高网站的访问速度,萨龙网络也将所有的媒体文件放置在二级域名下,WordPress3.5以上的版本,隐藏了后台的媒体(Media)设置页面…Sixianqiu主题是专为诗歌设计开发的主题,有一些朋友也需要这样的多形式博客主题,就整理出来了。一开始所有的文章内容都是居中显示,这样对于其…WordpressMU多站点已经使用很久,在本地测试和主机上安装的都是多站点,极大的方便网站搭建以及主题开发。当我们有多个站点,需要创建的新站…