Wordpress 博客“加载更多”博客文章索引创建博客重复

时间:2021-03-25 11:37:34

标签: ajax wordpress

我不是开发人员,我从事营销工作,但对 HTML 等有一些了解,

我们的博客文章索引页有问题。当我们发布了 9 个以上的博客时,会触发一个加载更多按钮。然后它加载下面的其他博客,似乎没有限制加载下面的显示数量,所以我假设它设置为加载所有剩余的博客。但是,当您单击加载更多时,它不仅会显示其他博客,而且还会一次又一次地复制相同的博客字符串,然后显示另一个加载更多按钮,重复的过程再次发生。

我一直在查看我们的函数文件、blog-index 和 myloadmore.js.txt,但根据我在各种论坛上阅读的内容,我看不出问题出在哪里。

在检查博客索引页面并查看网络选项卡时,它似乎确实加载了两次 admin-ajax.php 文件。同样在控制台选项卡上 myloadmore.js?ver=5.7:13 显示多次。不确定这是否是一个问题,以及这是否有助于突出问题?

如果能提供一些帮助,我将不胜感激,但请记住,这不是我的专业领域,因此您可能需要用更简单的术语来解释我需要去哪里以及我需要更改什么才能解决问题。< /p>

这是我的 blog.index:

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    
    <?
    // Get data of latest post of this blog type for Recent Featured Image
    $recent_posts = wp_get_recent_posts(array(
        'numberposts' => 1, // Number of recent posts thumbnails to display
        'post_status' => 'publish', // Show only the published posts
        'post_type'   => get_field('blog_post_type')
    ));
    
    foreach($recent_posts as $post) : 
        
        if ($post){
            $featImageURL = get_the_post_thumbnail_url($post['ID'], 'full');
            $featTitle = $post['post_title'];
            $featLink = get_permalink( $post['ID']);
            
            $categories = get_the_category($post['ID']);
            $featCategory = $categories[0]->name;
        }
    
    endforeach; 
    wp_reset_query(); ?>
    
    <div class="o-blogFeaturedImage lozad" data-background-image="<?= $image['url'] ?>" style="background-image: url(<?= $featImageURL; ?>);">

        <div class="container">
            <div class="row justify-content-<?= $feat['card_alignment']; ?>">
                <div class="col-12 col-lg-6 p-0">
                    <div class="m-blogFeaturedImage__card m-heroBannerWithCard__card blogIndex background -white text-center">
                        
                        <h6 class="subTitle"><?= $featCategory; ?></h6>
                        
                        <h3 class="title "><?= $featTitle; ?></h3>
                        
                        <div class="m-heroBannerWithCard__link">
                            <a class="a-featuredContentCards__readMore textButton" href="<?= $featLink; ?>">
                                Read More
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
    
    <div class="container">
        <div class="row justify-content-md-between">
            <div class="background text-center col-md-12">
                <div class="m-content-card__heading pb-4 pb-md-5">
                    <?php if($sub_heading) : ?>
                        <h6 class="subTitle"><?php echo $sub_heading; ?></h6>
                    <?php endif; ?>
                    <?php if($heading) : ?>
                        <h1 class="title2 pb-0 mb-0" style="color:<?php echo $heading_color?>"><?php echo $heading; ?></h1>
                    <?php endif; ?>
                    <hr>
                    <?php if($copy) : ?>
                    <div class="m-content-card__copy col-md-9 px-0 m-auto pb-2" style="color:<?php echo $heading_color?>">
                        <?php echo $copy; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <div class="m-blogListing">
            <? $postType = get_field('blog_post_type');
            
            $post_type = $postType;
            $taxonomy = 'category';
            $categoryNames = [];
            $arrCat = new ArrayObject();
            $arrTags = new ArrayObject();
            $finalArrObj = new ArrayObject();
            
            $args = array(
                    'post_type' => $postType,
                    'posts_per_page' => -1,
                    'post_status' => array('publish') // For testing purposes : Publish + Draft posts
                );

            $query = new WP_Query($args);

            if( $query->have_posts() ) {
                while( $query->have_posts() ) {
                    $query->the_post();

                    $terms = get_the_terms($post->ID, $taxonomy);
                    $tags = get_the_terms($post->ID, 'tags');
                    $categories = [];

                    // List of category for this specific post type
                    if( $terms ) {          
                        foreach ($terms as $category) {
                            //$categories[] = $category->name;
                            array_push($categoryNames, $category->name);
                            $arrCat->append( array($category->name, $category->term_id) );
                            $arrTags->append( array($tag->name, $tag->term_id) );
                        }       
                    }                   
                    
                    // List of tags for this specific post type
                    if( $tags ) {          
                        foreach ($tags as $tag) {
                            $arrTags->append( array($tag->name, $tag->term_id) );
                        }       
                    }
                }
            }
        

            // This section is for the Category/Tag Dropdown Filter
            $terms = get_field('blog_category');
            
            if( $postType ): ?>
            
            <div class="m-blogListing__filter background -white">
                <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
                    <select name="categoryfilter" class="m-blogListing__filCategory">
                        <option value="">All Products</option>
                        
                        <?                  
                        // Remove duplicates on the Array Object
                        my_array_unique($arrCat);
                        
                        for ( $i=0; $i < sizeof($arrCat); $i++){
                            $flag = 0;
                            foreach ($arrCat[$i] as $key => $value) {                   
                                if ($flag == 0)
                                echo '<option value=' . $arrCat[$i][1] . '>' . $arrCat[$i][0] . '</option>';
                                
                                $flag++;
                            }
                        }
                        
                        ?>
                    </select >
                    
                    <?
                    // Fetch all tags from given post type
                    global $post;
                    $loop = new WP_Query(array('post_type' => $postType, 'posts_per_page' => -1));
                    
                    while ($loop->have_posts()) : $loop->the_post();
                    $tags = wp_get_post_terms($post->ID, 'post_tag');          
                    
                    endwhile;
                    
                    // Get all tags from Posts -- 02/17/2021
                    $terms = get_terms( array(
                        'taxonomy' => 'post_tag',
                        'hide_empty' => false,
                    ) );
                    
                    // Populate all filtered $tags on the dropdown list Tag Filter
                    $html = '<select name="tagfilter" class="m-blogListing__filAllRanges">
                            <option value="">All Topics</option>';
                    foreach ( $terms as $tag ) { 
                        $html .= "<option value='$tag->term_id'>";
                        $html .= "{$tag->name}</option>";
                    }
                    $html .= '</select>';
                    echo $html;

                    ?>
                    
                    <button class="m-blogListing__searchBtn button">Search</button>
                    <input type="hidden" name="posttype" id="hiddenField" value="<?= $postType; ?>" />
                    <input type="hidden" name="action" value="myfilter">
                </form>
                
            </div>
        
            <?
            //  This section is for the Blog index grid
            ?>
            <div id="blog-posts" class="row">
                    <?  
                        $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
                    
                        $postsPerPage = 9;
                        $args = array(
                            'post_status' => array( 'publish'), // For testing purposes : Publish + Draft posts
                            'post_type' => $postType,
                            'posts_per_page' => $postsPerPage,
                            'paged' => $paged
                        );
                    
                    $query = new WP_Query( $args );
                    
                    if ( $query->have_posts() ):                    
                       while($query -> have_posts()) : $query -> the_post(); ?>
                       <?php if ( empty(get_field('externl_link', get_the_ID())) ){  ?>
                        <div class="col-12 col-md-4">
                            <div class="m-blogListing__item">   
                                
                                <?php if ( has_post_thumbnail() ) : ?>
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                        <div class="blogListing__featuredImage">
                                            <?php the_post_thumbnail('medium_large'); ?>
                                        </div>
                                    </a>
                                <?php endif; ?>
                                
                                <div class="blogListing__content background -white">
                                    <?
                                    $category = get_the_category(); 
                                    ?>
                                    <h6 class="subTitle"><?= $category[0]->cat_name; ?></h6>
                                    <h5 class="blogListing__contentTitle"><a href="<?php the_permalink(); ?>"><? the_title(); ?></a></h5>
                                    <a href="<?php the_permalink(); ?>" class="blogListing__contentReadmore textButton">Read More</a>
                                </div>
                            </div>
                        </div>
                       <? }
                       endwhile;
                    endif; 
                    
                    // Added this now 
                    wp_reset_query() ; 
                    
                    ?>
                
            </div>
            <div class="text-center">
            
                <div class="button d-none" data-category="<?= $cat_id; ?>" id="more_posts">Load More</div>          
                
                <?php
                
                // don't display the button if there are not enough posts
                // if (  $query->max_num_pages > 1 ): 
                    // echo '<div class="loadmore button">Load More</div>'; 
                
                // if there is more than 1 page of posts – display the button
                if ( $query->max_num_pages > 1 ) {
                    echo '<div class="loadmore button misha_loadmore" data-args="' . esc_attr( json_encode( $args ) ) . '" data-max-page="' . $query->max_num_pages . '" data-current-page="1">Load More</div>';
                }

                ?>
            </div>
            
            <?php endif; ?>
            
        </div>

    </div>
    
    <? if ( get_field('show_card') ): ?>
    
        <? $card = get_field('card');
        if( $card ): ?>
            <div class="container">
                <div class="row justify-content-<?= $card['card_alignment']; ?>">
                    <div class="col-12 col-lg-6 p-0">
                        <div class="m-heroBannerWithCard__card background -white ">
                            
                            <? $headerTag = $card['title_header_tag']; ?>
                            <? if ( $headerTag == 'h1') { $h2Helper = 'title2'; } ?>
                            
                            <?= '<' . $headerTag . ' class="' . $h2Helper . '">' . $card['title'] . '</' . $headerTag . '>'; ?>
                            
                            <div class="m-heroBannerWithCard__richtext">
                                <?= $card['richtext']; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <? endif; ?>
        
    <? endif; ?>
    
</div>

<script>
    var posts_myajax = '<?php echo serialize( $query->query_vars ) ?>',
    current_page_myajax = 1,
    max_page_myajax = <?php echo $query->max_num_pages; ?>;
</script>
<script src="<?= get_stylesheet_directory_uri(); ?>/template-parts/blocks/blog-index/myloadmore.js"></script>
`    

0 个答案:

没有答案