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年从江西骑行来到大理,现栖居于洱海边,食人间烟火,过简单生活,做简约设计!默认情况下WordPress是不允许重复的评论提交,主要是为了防止垃圾评论。有些情况下又需要重复评论,最近用户需要在MNews Pro主题上定制…当出现 "Allowed memory size of 134217728 bytes exhausted" 错误时,意味着 PHP 内存限制被…Woocommerce,一个wordpress的电子商务扩展插件,一个很漂亮的可以帮你卖任何东西的工具,这是插件在wordpress后台插件中的…在Wordpress主题开发的过程中,尤其是CMS主题分类的布局,通过后台选项(萨龙龙使用的都是Redux开源框架)选择分类,前台通过调用分类的…一般的Wordpress主题中都有多种自定义文章类型,除了了默认的文章还会有作品、商品等自定义类型的文章,而WordPress默认只能搜索到文章…萨龙网络Purity主题是集成了Woocommerce+Buddypress+Bbpress商城社区论坛,因为www.gravatar.com头…在开发 WordPress 主题中,需要 N 多次测试,这样就需要多次重装测试网站,这样相当的麻烦。今天了解到一款数据库初始化插件:WordPr…WordPress 能够在自定义菜单中存储每个菜单项的描述,默认情况下,这些描述不会显示在前端菜单中,需要为相应的菜单增加显示描述的功能。
一…Isotope是一个优秀的jQuery插件能够对一组页面元素进行筛选、排序、并创建智能布局。利用这个插件,能够对任意给定的一组元素用显示/隐藏效…描述
添加斜杠以转义字符串。
如果设置了magic_quotes_gpc,将首先删除斜杠。
用法
…Codestar Framework,一个轻量且易用的 WordPress 选项框架,在萨龙龙最新设计开发的企业教育培训类 WordPress …前面写过AMH面板安装配置redis加速WordPress站点一文,来介绍redis的安装与配置,对于WordPress站点的加速效果是相当的明…