簡體   English   中英

WordPress的文章類型無限滾動/加載更多按鈕

[英]Wordpress Post Type Infinite Scroll / Load more button

我正在使用以下代碼當前在WP上顯示指定數量(24)的自定義帖子類型,並使用項目類別動態篩選帖子。

            <!-- Display Filters for Posts --->
            <ul id="filters">
                <li><a href="#" data-filter="*" class="selected">Everything</a></li>
             <?php 
             $terms = get_terms("project_categories"); // get all categories, but you can use any taxonomy
             $count = count($terms); //How many are they?
             if ( $count > 0 ){  //If there are more than 0 terms
             foreach ( $terms as $term ) {  //for each term:
             echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
             //create a list item with the current term slug for sorting, and name for label
             }
             } 
             ?>
            </ul>

        <!-- Filter Array --->
        <div class="row">
            <div class="col-md-12">

                    <?php $args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 24 ); ?>

                    <?php $the_query = new WP_Query( $args ); ?>

                    <?php if ( $the_query->have_posts() ) : ?>
                        <ul class="img-list" id="isotope-list">
                        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
                     $termsArray = get_the_terms( $post->ID, "project_categories" );  //Get the terms for this particular item
                     $termsString = ""; //initialize the string that will contain the terms
                     foreach ( $termsArray as $term ) { // for each term 
                     $termsString .= $term->slug.' '; //create a string that has all the slugs 
                     }
                     ?> 
                     <li class="<?php echo $termsString; ?>item project home-project"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
                             <a href="<?php echo get_permalink( $post->ID ); ?>">
                                 <?php the_post_thumbnail(); ?>
                                 <span class="text-content"><span><img src="<?php the_field('client_logo'); ?>" alt="" class=""></span></span>
                            </a>
                     </li> <!-- end item -->
                        <?php endwhile;  ?>
                        </ul> <!-- end isotope-list -->
                    <?php endif; ?>

            </div>
        </div><!-- Close Filter Array --->

這是使用同位素過濾從這些指令中進行一些修改而構建的。 https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/

更新:在瑪麗莎評論之后,這就是我的代碼在過濾器之后的樣子。

<?php $args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 18 ); ?>

<?php $the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>
<ul class="img-list" id="isotope-list">

<?php $total_posts = wp_count_posts('bw_projects');
$total_posts = $total_posts->publish;
$number_shown = 0; ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
$termsArray = get_the_terms( $post->ID, "project_categories" );  //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term 
$termsString .= $term->slug.' '; //create a string that has all the slugs 
}
?> 
<li class="<?php echo $termsString; ?>item project home-project"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
     <a href="<?php echo get_permalink( $post->ID ); ?>">
         <?php the_post_thumbnail(); ?>
         <span class="text-content"><span><img src="<?php the_field('client_logo'); ?>" alt="" class=""></span></span>
    </a>
</li> <!-- end item -->
<?php endwhile;  ?>
</ul> <!-- end isotope-list -->
<?php endif; ?>
            <?php if ( $number_shown < $total_posts ) { ?>
<div class="loadMore" data-offset="<?php echo $number_shown; ?>" data-total="<?php echo $total_posts; ?>">
        Load More
</div>
<?php } ?>

我的loadmore文件處於以下當前狀態:

<?php
global $post;
$the_offset = trim( sanitize_text_field( wp_unslash( $_POST['pOffset'] ) ) );
$the_total = trim( sanitize_text_field( wp_unslash( $_POST['totalPosts'] ) ) );

$args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 12, 'offset' => $the_offset );
$posts_shown = $the_offset; //Increment this every time you display a project
?>

                <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
                     $termsArray = get_the_terms( $post->ID, "project_categories" );  //Get the terms for this particular item
                     $termsString = ""; //initialize the string that will contain the terms
                     foreach ( $termsArray as $term ) { // for each term 
                     $termsString .= $term->slug.' '; //create a string that has all the slugs 
                     }
                     ?> 
                     <li class="<?php echo $termsString; ?>item project home-project"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
                             <a href="<?php echo get_permalink( $post->ID ); ?>">
                                 <?php the_post_thumbnail(); ?>
                                 <span class="text-content"><span><img src="<?php the_field('client_logo'); ?>" alt="" class=""></span></span>
                            </a>
                     </li> <!-- end item -->
                        <?php endwhile;  ?>
                        </ul> <!-- end isotope-list -->
                <?php endif; ?>
<?php } ?>

<?php //Then check if we've shown all the posts or not, and we're done.
if ( $posts_shown >= $the_total ) { ?>
    <div id="all-posts-shown"></div>
<?php } ?>

在輸出的HTML上,“加載更多”按鈕引入“數據總計”和“數據偏移量”

<div class="loadMore" data-offset="0" data-total="25">
Load More
</div>

單擊加載更多時,偏移量更改為:

<div class="loadMore" data-offset="12" data-total="25">
Load More
</div>

除了我沒有看到額外的帖子加載? 我想我一定錯過了什么。

您可以使用ajax在按鈕單擊或滾動時拉出下一輪帖子。 如果您仍然無法掌握一切,則單擊按鈕的點擊會更容易跟蹤。

在循環顯示帖子之前,請確定您的自定義帖子類型中總共有多少個帖子,以及一個計數器來跟蹤您已顯示的帖子數。

$total_posts = wp_count_posts('bw_projects');
$total_posts = $total_posts->publish;
$number_shown = 0;

然后,每次顯示項目時都顯示增量編號。 然后,在循環之后,檢查是否有更多項目要顯示,然后添加一個loadmore按鈕,其數據屬性存儲偏移量(已顯示多少個帖子)和總數。

<?php if ( $number_shown < $total_posts ) { ?>
    <div class="loadMore" data-offset="<?php echo $number_shown; ?>" data-total="<?php echo $total_posts; ?>">
                Load More
    </div>
<?php } ?>

然后,您可以使用AJAX獲得下一篇文章。 您想先注冊您的AJAX函數:

<?php
/**
 * Put the following code in your functions.php file
 * get_loadmore is the name of the ajax function we are registering
 * projects-loadmore.php is the file with the contents which will be loaded 
 * when the ajax call is made. 
*/

            add_action('wp_ajax_get_loadmore', 'get_loadmore');
            add_action( 'wp_ajax_nopriv_get_loadmore', 'get_loadmore' );
            function get_loadmore() {

              include(get_template_directory().'/projects-loadmore.php');
              wp_die();

            }
?>

然后您要在按鈕單擊上設置ajax函數

<script>
/**---
    Notes:
    When loadMore button is clicked
    This passes information to and pulls content dynamically from the template: projects-loadmore.php
    The returned php and html code is inserted at the end of the #isotope-list div's content
    variables are pulled from the loadmore button's data attributes.
    Variables Passed:
        - postOffset : how many posts have already been displayed
        - totalPosts :  The total number of published posts in this post type.
---**/

$('.loadMore').click(function() {

    //get the offset
    var postOffset = $(this).attr('data-offset');
    var totalPosts = $(this).attr('data-total');
    var thisObj = $(this);

    //get Page Template and put on page
    var ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';

    $.ajax({
        type: 'POST',
        url: ajax_url,
        data: {
            action: 'get_loadmore',
            pOffset: postOffset,
            totalPosts: totalPosts,
        },
        dataType: "html",
        success: function(data) {
            $('#isotope-list').append(data);
            $new_offset = parseInt ( thisObj.attr('data-offset') ) + 24;
            thisObj.attr('data-offset', $new_offset) //set the new post offset.
            if ( $('#all-posts-shown')[0] ) {
                thisObj.slideUp(); //hide loadmore if all posts have been displayed.
           }
        },

        error: function(MLHttpRequest, textStatus, errorThrown){
            alert("error");
        }
    });
}); //end of click function 
</script>

然后剩下的就是進入您的projects-loadmore.php文件,獲取變量,調整查詢並顯示帖子:

<?php
global $post;
$the_offset = trim( sanitize_text_field( wp_unslash( $_POST['pOffset'] ) ) );
$the_total = trim( sanitize_text_field( wp_unslash( $_POST['totalPosts'] ) ) );

$args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 24, 'offset' => $the_offset );
$posts_shown = $the_offset; //Increment this every time you display a project
?>

//Get your posts and insert your while loop and display code here

<?php //Then check if we've shown all the posts or not, and we're done.
if ( $posts_shown >= $the_total ) { ?>
    <div id="all-posts-shown"></div>
<?php } ?>

在動畫和調整方面,您可以做很多事情,但這足以讓您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM