php - 将分页添加到 wp-query
问题描述
我试图在我的帖子查询中添加分页。
我尝试了我在互联网上找到的所有示例来添加分页但没有成功:/
这是我尝试的分页代码的查询代码:
<?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();
?>
我想让它每页显示 3 个帖子。这就是我在posts_per_page
参数中添加 3 的原因。
我想要一个编号列表。
解决方案
请使用此代码
<?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>
推荐阅读
- python - 找不到平台相关库
考虑将 $PYTHONHOME 设置为 [: ] 对于 Python 3.9 - r - R:根据条件将前导行值设置为 NA
- javascript - 如何在循环中在 html 画布上绘制图像
- php - 在 Ubuntu 20 上增加 PHP 的 memory_limit 不起作用
- python - 如何提高 .loc 在 Python 中附加数据帧的性能?
- jenkins - 通过 Jenkins 管道在 Jfrog 中发布的附加空白模块
- node.js - 发送 index.html 时从未触发主“/”路由控制器
- html - Angular中表格的操作列不起作用
- flutter - Image_Picker - 如果我使用拆分,图像不会显示
- python - 为什么我无法安装烧瓶(PIP 错误)?