[英]Add Pagination to wp-query
Im trying to add a pagination to my post query. 我正在尝试向我的帖子查询添加分页。
I tried all the samples i found in the internet to add a pagination without any success :/ 我尝试在互联网上找到的所有样本都添加分页,但没有成功:/
This is my query code with the pagination code I tried: 这是我的查询代码以及尝试的分页代码:
<?php
$count = 0;
// Query Code
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Recognitions', 'post_status'=>'publish', 'posts_per_page=3&paged=' . $paged));
if ( $wpb_all_query->have_posts() ) :
while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
?>
<?php $linktab = 'http://' . "localhost:8888/Lemon/wordpress/?page_id=158" ."&featuredimage=".get_the_post_thumbnail_url(); ?>
<div class="wraper-tab
<?php
$count++;
$current_image = $_GET["featuredimage"];
if (!isset($current_image) && $count == 1) {
$_GET["featuredimage"] = get_the_post_thumbnail_url();
$current_image = get_the_post_thumbnail_url();
}
if ($current_image == get_the_post_thumbnail_url()) {
echo "active";
}
?>
">
<a class="mitch-button to-load dark" href="<?php echo $linktab ?>" data-i="1" data-title="Lapka">
<article class="tab vertical-center" data-background-l="dark" data-background-p="dark" itemscope itemtype="http://schema.org/CreativeWork">
<header class="page-header caption">
<div>
<h6 class="tab-date"><?php echo get_the_date(); ?></h6>
<h2 class="title" itemprop="name"><?php the_title(); echo $count;?></h2>
</div>
</header>
<div class="media">
<img src="" />
</div>
</article>
</a>
</div>
<?php endwhile; ?>
<?php
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
?>
</div>
<?php
// Pagination code
next_posts_link();
previous_posts_link();
?>
I want to make so that it shows 3 posts per page. 我要使其显示每页3个帖子。 thats why I added 3 in the posts_per_page
parameter. 那就是为什么我在posts_per_page
参数中添加了3。
I want a numbered list. 我想要一个编号列表。
Please use this code 请使用此代码
<?php $count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Recognitions', 'post_status'=>'publish', 'posts_per_page'=>3, 'paged' => $paged )); $wpb_all_query->query_vars[ 'paged' ] > 1 ? $current = $wpb_all_query->query_vars[ 'paged' ] : $current = 1;
$pagination = array(
'base' => 'page/%#%',
//'format' => '',
'showall' => false,
'end_size' => 1,
'mid_size' => 6,
'total' => $wpb_all_query->max_num_pages,
'current' => $current,
'prev_text' => __('←'),
'next_text' => __('→'),
'type' => 'list'
);
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = str_replace('&page='.$current, '', $actual_link);
global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() )
$pagination[ 'base' ] = $url. '&page=%#%' ;
if ( $wpb_all_query->have_posts() ) :
while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
?>
<?php $linktab = 'http://' . "localhost:8888/Lemon/wordpress/?page_id=158" ."&featuredimage=".get_the_post_thumbnail_url(); ?>
<div class="wraper-tab
<?php
$count++;
$current_image = $_GET["featuredimage"];
if (!isset($current_image) && $count == 1) {
$_GET["featuredimage"] = get_the_post_thumbnail_url();
$current_image = get_the_post_thumbnail_url();
}
if ($current_image == get_the_post_thumbnail_url()) {
echo "active";
}
?>
">
<a class="mitch-button to-load dark" href="<?php echo $linktab ?>" data-i="1" data-title="Lapka">
<article class="tab vertical-center" data-background-l="dark" data-background-p="dark" itemscope itemtype="http://schema.org/CreativeWork">
<header class="page-header caption">
<div>
<h6 class="tab-date"><?php echo get_the_date(); ?></h6>
<h2 class="title" itemprop="name"><?php the_title(); echo $count;?></h2>
</div>
</header>
<div class="media">
<img src="" />
</div>
</article>
</a>
</div>
<?php endwhile; ?>
<?php
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
?>
</div><nav class="woocommerce-pagination"><?php echo paginate_links($pagination); ?></nav>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.