修改WordPress Jetpack插件使用'类'而​​不是'ID'属性

时间:2013-02-16 18:56:37

标签: php javascript wordpress infinite-scroll jetpack

我想要实现的目标:启用无缝&同时无限滚动同一页面上的多个列,每个列都提取不同的内容集,即一列显示最新帖子,而另一列显示特定标记的最新帖子。 / p>

每一列都使用不同的循环,这样它们就不会相互混淆,这就是我所使用的代码(只是为了让你了解我是如何做到的):

文件:index.php code also on pastebin

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package Twenty Twelve
 * @since Twenty Twelve 1.0
 */

get_header(); ?>

        <div id="primary" class="content-area">
            <section id="content" class="site-content" role="main">
                <?php if ( have_posts() ) : ?>
                    <?php //twentytwelve_content_nav( 'nav-above' ); ?>
                    <?php /* Start the Loop */ ?>
                    <?php while ( have_posts() ) : the_post(); ?>
                        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix content-articles'); ?>>
                            <a class="archives-thumb-link" href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_post_thumbnail( 'archives-thumb' ); ?></a>

                            <div class="entry-text-wrapper">
                                <header class="entry-header">
                                    <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
                                </header><!-- .entry-header -->
                            </div>
                        </article><!-- #post-<?php the_ID(); ?> -->
                    <?php endwhile; ?>
                    <?php twentytwelve_content_nav( 'nav-below' ); ?>
                <?php else : ?>
                    <?php get_template_part( 'no-results', 'index' ); ?>
                <?php endif; ?>
            </section><!-- #content .site-content -->

            <section id="highlights-container" class="site-content">
                <?php $latest_highlights = new WP_Query('tag=highlights&showposts=20&paged=' . $paged); ?>
                <?php if ( $latest_highlights->have_posts() ) : ?>
                    <?php while ($latest_highlights->have_posts()) : $latest_highlights->the_post(); $the_highlights_counter++; ?>
                        <article id="highlights-<?php echo $the_highlights_counter; ?>" class="highlights-wrapper">
                            <a class="highlights-link" href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark">
                                <?php the_post_thumbnail( 'highlights-thumb' ); ?>
                                <h1 class="highlights-title"><?php the_title(); ?> <span>/ <?php echo the_time('M. n'); ?></span></h1>
                            </a>
                        </article>
                    <?php endwhile; ?>
                <?php else : ?>
                    <?php get_template_part( 'no-results', 'index' ); ?>
                <?php endif; ?>
            </section><!-- #content .site-content -->
        </div><!-- #primary .content-area -->

<?php get_footer(); ?>

我打算怎么做: Jetpack中的“无限滚动”模块只构成两个文件 - infinity.php {{3 } ,所以对于那些了解JavaScript和PHP的人来说,它会更容易理解。

现在的事情是,infinity.js,要启用无限滚动,我们需要首先为它提供“无限滚动应添加其他帖子的HTML元素的ID。”由于它不接受多个ID,因此无法在同一页面上的多个列上同时进行无限滚动。

这个想法:所以,如果我修改它以接受class而不是id属性,我可以在多列上无限滚动工作

问题是,我该怎么做?


尽我所能尽力解决问题(我不能),但我遇到了一些重要的问题,我认为这会使你更容易提供帮助。

[infinity.php][5]

  • 'container' => 'content'表示content是容器元素的默认ID。

  • 'id' => self::get_settings()->container,为要调用的JavaScript定义id

[infinity.js][6]

  • var id = $( this ).attr( 'id' ),确保它是id属性,而不是class

因为,我不知道JS和足够的PHP,我可能错过了许多其他重要的部分。只是认为这些信息可以帮助那些试图提供帮助的人。

如果我不清楚,请告诉我。

注意:如果您正在某个地方运行测试/开发WordPress网站,并希望提供帮助,请安装 as stated here 插件( Slim Jetpack的版本,不需要您连接到WordPress.com),并从'Jetpack'菜单启用'无限滚动'模块。可以找到进一步的说明Jetpack plugin

3 个答案:

答案 0 :(得分:1)

Jetpack runs a partial template for the loop (e.g. content.php),通过AJAX检索HTML输出,然后将其“实时”附加到当前页面。它从不使用原始模板(index.php),所以你在那里编写了2个不同的循环并不重要。

您可以通过借用Jetpack的滚动检测和AJAX请求并调整其余的来编写自己的解决方案。

答案 1 :(得分:0)

您的服务器是否允许您在两个不同的端口上侦听请求?在进入视图时过滤掉你不想要的对象?

答案 2 :(得分:0)

如何使用不同的ID将scroll事件绑定到四个Ajax调用。

除此之外,您可以尝试重写插件,有人会讨论Here

相关问题