我想创建一个更多加载按钮。
我使用get_categories
来显示我的类别。 (我有一个电视节目网站,所以我使用类别来显示系列,帖子来自剧集。Example)
这是我的代码,可以帮我将分页更改为加载更多按钮吗?
<?php
$args = array(
'orderby' => 'cat_name',
'hide_empty' => 1,
'hierarchical' => 1,
'parent' => '0'
);
$categories = get_categories($args);
$numOfItems = 25;
$page = isset( $_GET['pagina'] ) ? abs( (int) $_GET['pagina'] ) : 1;
$to = $page * $numOfItems;
$current = $to - $numOfItems;
$total = sizeof($categories);
for ($i=$current; $i<$to; ++$i) {
$category = $categories[$i];
if ($category->name) {
echo '<a class="cover margin-ultimos" href="' . get_category_link( $category->term_id ) . '">';
echo '<div id="cover-home" class="gray-shadow">';
echo '<img src="'. get_field( 'portada', 'category_'.$category->term_id ). '" alt="Portada '. get_cat_name ( $category->term_id ) . '" />';
echo '</div>';
if ( strlen(get_cat_name ( $category->term_id )) > 20 ) { echo '<p class="text-ultimos menor">'.substr(get_cat_name ( $category->term_id ), 0, 20).'...</p>'; } else { echo '<p class="text-ultimos menor">'.get_cat_name ( $category->term_id ).'</p>';}
echo '<p class="last-info">'.$category->category_count.' Episodios</p></a>';
}
}
unset($category);
echo '<div class="clr"></div>';
echo '<div class="paginacion">';
echo paginate_links( array(
'base' => add_query_arg( 'pagina', '%#%' ),
'format' => '',
'prev_text' => __('<i class="fa fa-chevron-left"></i> Anterior'),
'next_text' => __('Siguiente <i class="fa fa-chevron-right"></i>'),
'total' => ceil($total / $numOfItems),
'current' => $page
));
echo '</div>';
?>
提前致谢
答案 0 :(得分:2)
你的html分页是:
<div class="paginacion"><span class="page-numbers current">1</span>
<a class="page-numbers" href="/?pagina=2">2</a>
<a class="page-numbers" href="/?pagina=3">3</a>
<a class="next page-numbers" href="/?pagina=2">Siguiente <i class="fa fa-chevron-right"></i></a></div>
请用“加载更多”更改,可能是这样的:
<div class="paginacion" id="loadmore">Load More</div>
使用jquery创建事件处理程序并使用ajax:
$(document).ready(function(){
// count length catalog diplay first
var start = $("#ultimos-episodios > a").size();
var offset = start*2;
var clone = $("#ultimos-episodios > a").eq(0).clone();
$('#loadmore').click(function(){
$.ajax({
url:"linktoyourphpcategory.php",
dataType:"json",
type:"post",
data:{start_p:start,offset_p:offset},
success:function(data) {
clone.find('a').attr('href',data.href);
clone.find('img').attr('src',data.src);
clone.find('img').attr('alt',data.alt);
clone.find('p').eq(0).text(data.title);
clone.find('p').eq(1).text(data.episode);
// add that
$("#ultimos-episodios").append(clone);
//reset clone
clone = clone.clone();
// update start and offset
start = start+start;
offset = offset+start;
}
});
});
});
最后,在您的PHP代码中,可能是这样的:
$numOfItems = $_POST['start_p'];
$offset= $_POST['offset_p'];
$args = array(
'orderby' => 'cat_name',
'hide_empty' => 1,
'hierarchical' => 1,
'parent' => '0',
'offset' => $offset,
'limit' => $numOfItems
);
$categories = get_categories($args);
$item = array();
foreach($category as $cat){
$item[]= array("href" => $cat->'yourlink',
"src" => $cat->'yoursourcefile',
"alt" => $cat->'yourtitlealtcategory',
"title" => $cat->'yourtitlecategory',
"episode" => $cat->'yourepisode');
}
echo json_encode($item);
答案 1 :(得分:0)
我找到了解决问题的方法。
所以,这是我的解决方案,也许这有点帮助。:
<强>的JavaScript 强>
var cvr = 1;
function caverload(){
$('#textito').hide();
$('#cargando').show();
$.ajax({
type: 'get',
url: '/index.php',
data: 'pagina='+(cvr+1),
success: function(html){
a = html.split('<span id="spliteado">');
b = a[1].split('</span>');
(b[0]==='' || b[0].length<20 || b[0]===null)? $('#nohaymas').show() : $('#ultimos-episodios').append(b[0]);
cvr = (cvr+1);
},
complete: function(){
$('#cargando').hide();
$('#textito').show();
}
});
return false;
}
<强> PHP 强>
<span id="spliteado">
<?php
$args = array(
'orderby' => 'cat_name',
'hide_empty' => 1,
'hierarchical' => 0,
'parent' => '0'
);
$categories = get_categories($args);
$numOfItems = 20;
$page = isset( $_GET['pagina'] ) ? abs( (int) $_GET['pagina'] ) : 1;
$to = $page * $numOfItems;
$current = $to - $numOfItems;
$total = sizeof($categories);
for ($i=$current; $i<$to; ++$i) {
$category = $categories[$i];
if ($category->name) {
echo '<a class="cover margin-ultimos" href="' . get_category_link( $category->term_id ) . '">';
echo '<div id="cover-home" class="gray-shadow">';
echo '<img src="'. get_field( 'portada', 'category_'.$category->term_id ). '" alt="Portada '. get_cat_name ( $category->term_id ) . '" />';
echo '</div>';
if ( strlen(get_cat_name ( $category->term_id )) > 20 ) { echo '<p class="text-ultimos menor">'.substr(get_cat_name ( $category->term_id ), 0, 20).'...</p>'; } else { echo '<p class="text-ultimos menor">'.get_cat_name ( $category->term_id ).'</p>';}
echo '<p class="last-info">'.$category->category_count.' Episodios</p></a>';
}
}
echo '</span>';
unset($category);
echo '<div class="clr"></div>';
echo '<div class="lalala">';
echo paginate_links( array(
'base' => add_query_arg( 'pagina', '%#%' ),
'format' => '',
'prev_text' => __('A'),
'next_text' => __('S'),
'total' => ceil($total / $numOfItems),
'current' => $page
));
echo '</div>';
?>
</div>
<div class="clr"></div>
<div id="caverload" class="paginacion" onclick="return caverload();">
<a id="textito">Cargar Más Series</a>
<div id="cargando" style="display:none">
<a style="padding-bottom: 2px; padding-right: 117px; padding-left: 117px;">
<img src="http://a.disquscdn.com/1427500112/img/loader.gif"/>
</a>
</div>
<div id="nohaymas" style="margin-top: -20px;display:none">
<a>Eso es Todo Amigos</a>
</div>
</div>
我拆分内容,点击按钮。
您可以展示它是如何运作的,here
无论在哪里,非常感谢您对所有评论者的帮助