php - 将分页添加到 wordpress 简码
问题描述
按照简码中关于Wordpress 分页的建议,我正在为 wordpress 编写简码并尝试为其添加分页。我快到了,只是不知道为什么 wordpress 不更新分页页面内容,例如,如果我单击“下一步”或“1”、“2”、“3”等,我总是看到相同的页面。
这是我的代码:
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'posts_per_page'=>$show,
'paged' => $paged,
'offset' => 0
));
while ($loop->have_posts()) : $loop->the_post();
global $paged; ?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
wp_reset_query();
?>
</div>
<?php }
有什么建议吗?:) 谢谢!
解决方案
添加functions.php
// Numbered Pagination
if ( !function_exists( 'kris_pagination' ) ) {
function kris_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
短代码
<?php kris_pagination(); ?>
推荐阅读
- python - 如何使用 aioresponses 模拟 ClientSession 而不将会话作为参数传递
- google-drive-api - 从 Python 读取 Google 表格中的评论所需的身份验证范围是什么
- r - 使用 multcomp::glht() 通过交互 Tukey 分析为 2 路方差分析生成 K 矩阵
- java - JavaFX ProgressIndicator 显示一个新的 Stage()
- python - 从数据框格式的文本列中提取单词
- sql-server - SQL查询结果重复
- flutter - 使用 flutter_driver 查找颤振三明治菜单按钮
- vue.js -
组件在使用 vue-server-renderer 的 Vue 服务器端渲染中不起作用 - android - Android活动过渡启动器背景闪烁
- r - 介数()函数igraph社交网络分析R中的错误