如何在wordpress网站添加瀑布流单页模板

我想在wordpress主题里加入一个瀑布流单页,效果如图。在网上曾经找过一些资料,说利用jQuery Masonry 插件来做,但没能做出来,希望如果对这方面比较熟悉的朋友可以告知。赏50金币,嘻嘻

<?php
/*
TEMPLATE NAME:瀑布流测试
 */
//基于Wordpress自带的2014主题
//贡献者:Suifengtec  suoling.net
if (! function_exists('coolwp_scripts_masonry') ) :
if ( ! is_admin() ) :
function coolwp_scripts_masonry() {
    //deregister built in masonry since it is old version 3.
   wp_deregister_script('jquery-masonry');
   //请自行下载
    wp_enqueue_script('jquery-10.1.js', get_template_directory_uri().'/test/jquery.js', array( 'imagesLoaded'), null, true );
       //请自行下载
    wp_enqueue_script('imagesLoaded', get_template_directory_uri().'/test/imagesloaded.pkgd.min.js', false, null, true);
       //请自行下载
    wp_enqueue_script('jquery-masonry', get_template_directory_uri().'/test/masonry.pkgd.min.js', array( 'imagesLoaded'), null, true );
       //后面我贴出来
    wp_enqueue_script('custom.js', get_template_directory_uri().'/test/custom.js', array( 'imagesLoaded'), null, true );
}

//wp_enquqe_style('masonry', get_template_directory_uri().'/test/');

add_action( 'wp_enqueue_scripts', 'coolwp_scripts_masonry' );
endif; //! is_admin()
endif; //! coolwp_scripts_masonry exists?
get_header(); ?>

<div id="main-content" class="main-content">

<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">

<?php
$args=array( 'post_type'=>'post' );
$myloop=new WP_Query( $args );
// Start the Loop.
while ( $myloop->have_posts() ) :  $myloop->the_post();

// Include the page content template.
?>

<div class="item">
<?php
 if ( has_post_thumbnail() ) : ?>
    <div class="masonry-thumbnail">
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
        <?php the_post_thumbnail('thumbnail'); ?></a>
    </div><!--.masonry-thumbnail-->
<?php endif; ?>
    <div class="masonry-details">
        <h5><a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><span class="masonry-post-title"> <?php the_title(); ?></span></a></h5>
       <!--  <div class="masonry-post-excerpt">
           <?php the_excerpt(); ?>
       </div> --><!--.masonry-post-excerpt-->
    </div><!--/.masonry-entry-details -->
</div><!--/.item -->
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php //get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();


custom.js代码:

var container = document.querySelector('#content');
var msnry = new Masonry( container, {
  columnWidth: 200,
  itemSelector: '.item'
});

/*alert('aaaaa');*/


将你下载到的三个js文件和上面的custom.js放在你的WP的2014主题目录下的test文件夹(自己建),然后将上面的PHP代码复制到一个英文开头的PHP文件里,把这个PHP文件放在WP的2014主题目录下,

启用这个主题,发布页面,选用“瀑布流测试”页面模板,如果你的测试WP中有文章,并且文章有缩略图,就会是这个样子:


只需要你改下查询语句和css,就可以套用到别的主题了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-02-13
这种需求没有主题能满足,因为一般主题都是全站瀑布流。
建议懂代码的话,自己改page模板,自己做瀑布流。
第2个回答  2021-03-03

WP开发21:wordpress网站文章页模板,如何调用文章的标题、内容、标签等信息?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网