Estoy usando un plugin para WordPress llamado Flex Mag Option, una de sus características es que agrega un infinite_scroll
(desplazador infinito) a la página inicial. Este no es muy personalizable desde el panel de configuraciones del tema. Así que tuve que entrar al codigo para tratar de arreglar un error que hace que se repita los posts que cargan. Me explico:
Se muestra una lista de posts (5 posts) y un botón de ver más, que al clickearlo se deben desplegar más posts debajo de los ya mostrados. Pero se vuelven a mostrar los mismos que ya aparecían.
El botón utiliza una función llamada pagination, abajo del código principal dejaré la función.
Aqui les dejo el código de la parte en cuestión:
<ul class="blog-widget-list left relative infinite-content">
<?php if (isset($do_not_duplicate)) { ?>
<?php global $post; $mvp_posts_num = esc_html(get_option('mvp_posts_num')); $paged = (get_query_var('page')) ? get_query_var('page') : 1; query_posts(array( 'posts_per_page' => $mvp_posts_num, 'post__not_in'=>$do_not_duplicate, 'paged' =>$paged )); if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="infinite-post">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>
<div class="blog-widget-img left relative">
<?php the_post_thumbnail('mvp-mid-thumb', array( 'class' => 'reg-img' )); ?>
<?php the_post_thumbnail('mvp-small-thumb', array( 'class' => 'mob-img' )); ?>
<?php $post_views = get_post_meta($post->ID, "post_views_count", true); if ( $post_views >= 1) { ?>
<div class="feat-info-wrap">
<div class="feat-info-views">
<i class="fa fa-eye fa-2"></i> <span class="feat-info-text"><?php mvp_post_views(); ?></span>
</div><!--feat-info-views-->
<?php $disqus_id = get_option('mvp_disqus_id'); if ( ! $disqus_id ) { if (get_comments_number()==0) { } else { ?>
<div class="feat-info-comm">
<i class="fa fa-comment"></i> <span class="feat-info-text"><?php comments_number( '0', '1', '%' ); ?></span>
</div><!--feat-info-comm-->
<?php } } ?>
</div><!--feat-info-wrap-->
<?php } ?>
<?php if ( has_post_format( 'video' )) { ?>
<div class="feat-vid-but">
<i class="fa fa-play fa-3"></i>
</div><!--feat-vid-but-->
<?php } ?>
</div><!--blog-widget-img-->
<?php } ?>
<div class="blog-widget-text left relative">
<span class="side-list-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span>
<h2><?php the_title(); ?></h2>
<p><?php echo wp_trim_words( get_the_excerpt(), 22, '...' ); ?></p>
</div><!--blog-widget-text-->
</a>
</li>
<?php endwhile; endif; ?>
<?php } else { ?>
<?php $mvp_posts_num = esc_html(get_option('mvp_posts_num')); $paged = (get_query_var('page')) ? get_query_var('page') : 1; query_posts(array( 'posts_per_page' => $mvp_posts_num, 'paged' =>$paged )); if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="infinite-post">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { ?>
<div class="blog-widget-img left relative">
<?php the_post_thumbnail('mvp-mid-thumb', array( 'class' => 'reg-img' )); ?>
<?php the_post_thumbnail('mvp-small-thumb', array( 'class' => 'mob-img' )); ?>
<?php $post_views = get_post_meta($post->ID, "post_views_count", true); if ( $post_views >= 1) { ?>
<div class="feat-info-wrap">
<div class="feat-info-views">
<i class="fa fa-eye fa-2"></i> <span class="feat-info-text"><?php mvp_post_views(); ?></span>
</div><!--feat-info-views-->
<?php $disqus_id = get_option('mvp_disqus_id'); if ( ! $disqus_id ) { if (get_comments_number()==0) { } else { ?>
<div class="feat-info-comm">
<i class="fa fa-comment"></i> <span class="feat-info-text"><?php comments_number( '0', '1', '%' ); ?></span>
</div><!--feat-info-comm-->
<?php } } ?>
</div><!--feat-info-wrap-->
<?php } ?>
<?php if ( has_post_format( 'video' )) { ?>
<div class="feat-vid-but">
<i class="fa fa-play fa-3"></i>
</div><!--feat-vid-but-->
<?php } ?>
</div><!--blog-widget-img-->
<?php } ?>
<div class="blog-widget-text left relative">
<span class="side-list-cat"><?php $category = get_the_category(); echo esc_html( $category[0]->cat_name ); ?></span>
<h2><?php the_title(); ?></h2>
<p><?php echo wp_trim_words( get_the_excerpt(), 22, '...' ); ?></p>
</div><!--blog-widget-text-->
</a>
</li>
<?php endwhile; endif; ?>
<?php } ?>
</ul>
<?php $mvp_infinite_scroll = get_option('mvp_infinite_scroll');
if ($mvp_infinite_scroll == "true") {
if (isset($mvp_infinite_scroll)) { ?>
<a href="#" class="inf-more-but">Ver más</a>
<?php } } ?>
<div class="nav-links">
<?php if (function_exists("pagination")) { pagination($wp_query->max_num_pages); } ?>
</div><!--nav-links-->
Pagination:
if ( !function_exists( 'pagination' ) ) {
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span>".__( 'Page', 'mvp-text' )." ".$paged." ".__( 'of', 'mvp-text' )." ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« ".__( 'First', 'mvp-text' )."</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ ".__( 'Previous', 'mvp-text' )."</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">".__( 'Next', 'mvp-text' )." ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>".__( 'Last', 'mvp-text' )." »</a>";
echo "</div>\n";
}
}
}